Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Simple Archive Command

19 REPLIES 19
SOLVED
Reply
Message 1 of 20
mnilson
732 Views, 19 Replies

Simple Archive Command

I feel like this is a simple request, but I'm not at all familiar with LISP programming... Would appreciate any help you can offer.

 

I'd like a command, "bak," which will do the following:

1. Check if "bak" folder exists in directory of current drawing.

2. If so, keep going. If not, create directory.

3. Save a copy of the current file to the "bak" directory, appending the current date and time to the file name.

 

The file that remains open at the end of command should still be the original (un-archived) version.

 

Many thanks in advance!

19 REPLIES 19
Message 2 of 20
Anonymous
in reply to: mnilson

Christmas comes early. See Autolisp program below. Only issue is I had to use "-" as a delimeter for the date and "." as a delimeter for the time. Autocad does not accept "\" or ":" in file names. Give it a try. Any issues, please let me know

 

BAK.LSP l. gabriel 12/23/11
;
;object: to create archive files in "current directory\BAK\filename date time.dwg
;        requested by mnilson   

;Date Function

(defun TODAY ( / d yr mo day)
  (setq d (rtos (getvar "CDATE") 2 6)
        yr (substr d 3 2)
        mo (substr d 5 2)
        day (substr d 7 2)
  )
  (setq str (strcat mo "-" day "-" yr))
  (setq str str)
)

;Time Function


(defun TIME ( / d hr m s)
  (setq d (rtos (getvar "CDATE") 2 6)
        hr (substr d 10 2)
        m (substr d 12 2)
        s (substr d 14 2)
  )
  (setq str (strcat hr "." m "." s))
  (setq str str)
)

;Main Program

(defun c:BAK()
   (setq directory (strcat (getvar "DWGPREFIX") "BAK"))
   (if (= (findfile directory) nil)
      (vl-mkdir directory)     
   )
   (setq archivename (strcat (substr (getvar "DWGNAME") 1 (- (strlen (getvar "DWGNAME")) 4)) " " (TODAY) " " (TIME)))
   (command "-WBLOCK" (strcat directory "\\" archivename) "*")
)

Message 3 of 20
mnilson
in reply to: Anonymous

Thanks much! That appears to do the trick, even with civil 3d objects and data references! I'm curious what made you choose wblock as opposed to some form of "save as"?  Only follow-up question is if there's a way to automatically accept ("Yes") the Autodesk Map Info prompt?  Thanks again.

Message 4 of 20
pbejse
in reply to: mnilson


@mnilson wrote:

I feel like this is a simple request, but I'm not at all familiar with LISP programming... Would appreciate any help you can offer.

 

I'd like a command, "bak," which will do the following:

1. Check if "bak" folder exists in directory of current drawing.

2. If so, keep going. If not, create directory.

3. Save a copy of the current file to the "bak" directory, appending the current date and time to the file name.

 

The file that remains open at the end of command should still be the original (un-archived) version.

 

Many thanks in advance!



Two thiings:

You can "copy" the current file name to the folder "bak" on its last saved state  or

to ensure you have the latest copy, you can invoke "saveas" to that folder and revert it back to its orignal name and folder

 

Which one do you prefer?

 

 

 

 

Message 5 of 20
mnilson
in reply to: pbejse

pbejse - The first... The intent is to simplify an archive process, since users sometimes realize that they neeed to do a backup only after getting in and making a few changes... So copying / renaming the last saved state, then reverting to the original file name and directory would seem appropriate. 

Message 6 of 20
pbejse
in reply to: mnilson


@mnilson wrote:

pbejse - The first... The intent is to simplify an archive process, since users sometimes realize that they neeed to do a backup only after getting in and making a few changes... So copying / renaming the last saved state, then reverting to the original file name and directory would seem appropriate. 



One more question? How ae you planning to invoke this routine? during startup? or a call from commnad prompt?

 

Message 7 of 20
mnilson
in reply to: pbejse


@pbejse wrote:

@mnilson wrote:

pbejse - The first... The intent is to simplify an archive process, since users sometimes realize that they neeed to do a backup only after getting in and making a few changes... So copying / renaming the last saved state, then reverting to the original file name and directory would seem appropriate. 



One more question? How ae you planning to invoke this routine? during startup? or a call from commnad prompt?

 


call from command prompt. ideally, "bak"

Message 8 of 20
pbejse
in reply to: mnilson


@mnilson wrote:

call from command prompt. ideally, "bak"


Try this:

 

 

(defun c:Bak (/ aDoc Path fn p)
(vl-load-com)
(cond ((and
	(setq aDoc (vla-get-ActiveDocument (vlax-get-acad-object)))
	(not (eq  (setq fn (vla-get-fullname aDoc)) ""))
        (progn
          (if (member
                "BAK"
                (mapcar	
                  'strcase
                  (vl-directory-files (setq p (vla-get-path aDoc)) nil -1)
                  )
                )
            (setq path (strcat (vla-get-path aDoc) "\\Bak\\"))
            (vl-mkdir
              (setq path (strcat (vla-get-path aDoc) "\\Bak\\"))
              )
            )
          path
          )
	(vl-file-copy fn
		(strcat path (vla-get-name aDoc)))
        )))
  (princ
  (if p
    (strcat "\n" path (vla-get-name aDoc))
    "\nDrawing never been saved"))
  (princ)
  )

 

 

 

If succesfulll, the full path will be shown on the command prompt:

 

 

Message 9 of 20
mnilson
in reply to: pbejse

Thank you both for your help... I can see benefits to both of these routines and plan to work with them more after the holidays.  Many thanks, and Merry Christmas / happy holidays!

Message 10 of 20
pbejse
in reply to: mnilson


@mnilson wrote:

Thank you both for your help... I can see benefits to both of these routines and plan to work with them more after the holidays.  Many thanks, and Merry Christmas / happy holidays!



Happy Holidays to you too..

 

BTW: can you handle the Date and Tiem Stamp suffix on the filename?

 

Anyhoo:

 

(vl-file-copy fn
  (setq path (strcat path  
                  (vl-filename-base fn) "_"(strcase (menucmd "M=$(edtime,$(getvar,date),MO-DD-YY-H_MM-am/pm)")) ".dwg" )))
        )))
  (print
  (if p
    path
    "\nDrawing never been saved"))
  (princ)
  )

 

Have fun

 

 

Message 11 of 20
Lee_Mac
in reply to: mnilson

My version:

 

 

(defun c:bak nil
    (cond
        (   (= 1 (getvar 'DWGTITLED))
            (vl-mkdir (strcat (getvar 'DWGPREFIX) "Bak"))
            (vl-file-copy
                (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME))
                (strcat (getvar 'DWGPREFIX) "Bak\\"
                    (cadr (fnsplitl (getvar 'DWGNAME)))
                    (menucmd "m=$(edtime,$(getvar,DATE), YYYY-MO-DD HH-MM)")
                    ".dwg"
                )
            )
            (princ "\nBackup Complete.")
        )
        (   (princ "\nDrawing not Saved.")   )
    )
    (princ)
)
Message 12 of 20
pbejse
in reply to: Lee_Mac


@Lee_Mac wrote:

My version:......

  


Geez,.. testing for "bak" folder is indeed ia moot point.... should have caught that.

 

Happy Holidays Lee Mac. Smiley Happy

 


 

Message 13 of 20
mnilson
in reply to: Lee_Mac

Thanks! I was going to cut and paste to get the date function in, but this does the trick!  Great website, Lee Mac... I just poked around a bit and have already learned something!

 

I appreciate all the help you folks offer to non-programmers such as myself! Now if only I had asked this question a couple years ago when I first thought about it!

Message 14 of 20
pbejse
in reply to: mnilson

FWIW: <update code>

 

 

Message 15 of 20
Anonymous
in reply to: mnilson

Glad it worked out for you. I chose "wblock" because it allows me to create the archive file without leaving the orignal drawing file. "Save As" keeps the saved file on the screen and closes the original.

 

I looked over the alternate program and it looks interesting as well. One day I will try to get into Vlisp functions, but after using the original Autolisp function for over 20 years, old habits are hard to break.

 

Always goes to show there is a more than one solution to any problem 

Message 16 of 20
Lee_Mac
in reply to: pbejse


pbejse wrote:

Happy Holidays Lee Mac. Smiley Happy


 

Thanks pbejse, same to you and yours Smiley Happy

 


mnilson wrote:

Thanks! I was going to cut and paste to get the date function in, but this does the trick!  Great website, Lee Mac... I just poked around a bit and have already learned something!

 

I appreciate all the help you folks offer to non-programmers such as myself! Now if only I had asked this question a couple years ago when I first thought about it!



Thanks mnilson! I'm delighted that you find my site beneficial, and of course, if you find anything on there that you don't understand, I'd be happy to put together an explanation for you.

 

Merry Christmas and Best Wishes for the New Year,

 

Lee

Message 17 of 20
Kent1Cooper
in reply to: pbejse


@pbejse wrote:
....
....to ensure you have the latest copy, you can invoke "saveas" to that folder and revert it back to its orignal name and folder

....

@Anonymous wrote:

....I chose "wblock" because it allows me to create the archive file without leaving the orignal drawing file. "Save As" keeps the saved file on the screen and closes the original.

....


It looks like there's a solution already using other methods, but just for your information / future reference:

 

There is a not-very-well-known command called plain SAVE [not to be confused with SAVEAS, nor with QSAVE, which is actually what you get from the toolbar disk icon and the so-called "Save" item on the File pull-down menu].

 

SAVE is somewhat like SAVEAS in that it creates another drawing file out of the one you're in, but whereas SAVEAS leaves you in the new file, SAVE keeps you in the old one.  There's no need to revert back to it, as you would need to do if you used SAVEAS, because you never leave it -- you just make a copy of it elsewhere.  And it's simpler than Wblock, because it requires no object selection -- it will do directly just what lgabriel describes as a reason for using Wblock: "create the archive file without leaving the original drawing file."

Kent Cooper, AIA
Message 18 of 20
pbejse
in reply to: Kent1Cooper

You have a point there kent, but by doing so it will save a copy on its current state which means its will not be a copy of the original "unsaved" state.

 

Hence i opted for vl-file-copy, however its ture only when the user hasnt invoke a save at all during the current session.

with that condition a reactor comes to mind.

 

BTW:

Make sure you set expert to 5 to suppress the prompt " Do you want to replace it? <N>"

 

EDIT: Better yet... make a copy of the ".bak" fie (if it exist) l to ".dwg" a small tweak on the updated code can achived that.

 

(vl-file-copy

(if (setq fnb (findfile (strcat (cadr (fnsplitl fn)) ".bak")))
            fnb fn) ....

Message 19 of 20
stevor
in reply to: pbejse

And consider the Autosave, SAVETIME, etc.

S
Message 20 of 20
pbejse
in reply to: stevor


@stevor wrote:

And consider the Autosave, SAVETIME, etc.



But of course, thats why  i would go for reactor for this task to handle all those issues  Smiley Wink

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost