Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 1 of 16 (354 Views)
Accepted Solution

move bak files to project's subfolder

354 Views, 15 Replies
01-24-2014 08:29 AM

Hey guys,

 

I'm always annoyed with the many .bak files within a projects folder and manually move it to a subfolder to bring everything in order.

Now I was thinking that this whould be way easier if this would work with lisps, the only thingis that I have no clue how! :smileyfrustrated:

 

I know, that there is the simple way, to move all .bak files to one global folder with the express tool "movebak", but I don't want to put it in one folder only.

 

my project is for example located in "c:\project_a\plan_a.dwg", so I want to create a subfolder in "c:\project_a\bak\plan_a.bak"

 

maybe you lisps-genii can help me with this issue! :smileyhappy:

 

cheers klingi

Member
Posts: 4
Registered: ‎01-24-2014
Message 2 of 16 (342 Views)

Re: move bak files to project's subfolder

01-24-2014 10:34 AM in reply to: Klingi162

Hi Klingi.


I am Brazilian and my English is very bad (I'm sorry for that).
I did here a little routine to do what you asked. I hope that is what you were needing.

 

graciously 
Gilson

 

 

(defun grab_bak_file ( / path name name_bak path_bak)
(setq path (getvar "dwgprefix"))
(setq name (getvar "dwgname"))
(setq name_bak (strcat (vl-filename-base name) ".bak"))
(setq path_bak (strcat path name_bak))
;Check to file exists
(if (vl-file-size path_bak)
path_bak
nil
)
)

(defun move_bak_file (path / temp_name temp_path dir_bak)
(setq temp_name (vl-filename-base path))
(setq temp_path (vl-filename-directory path))
;Check to dir bak exists
(if (not (setq dir_bak (vl-directory-files temp_path "bak" -1)))
(if (not (vl-mkdir (strcat temp_path "\\bak")))
(progn
(alert "\nError: Error to creat dir bak")
(exit)
)
)
)
(setq dir_bak (strcat temp_path "\\bak\\"))
;Delete bak old
(vl-file-delete (strcat dir_bak temp_name))

(if (not (vl-file-rename path (strcat dir_bak temp_name)))
(progn
(alert "\nError: Error to move file bak")
(exit)
)
)
)

(defun c:movebak ( / decho_old path_bak)

(setq decho_old (getvar "cmdecho"))
(setvar "cmdecho" 0)

;Function grab file .bak with same name
(setq path_bak (grab_bak_file))
(if path_bak
(progn
(move_bak_file path_bak)
(prompt "\nFile moved")
)
(prompt "\nNot file to move")
)
(setvar "cmdecho" decho_old)
)

(prompt "\nmovebak - to move bak file")

 

 

*Expert Elite*
Lee_Mac
Posts: 1,038
Registered: ‎12-29-2009
Message 3 of 16 (340 Views)

Re: move bak files to project's subfolder

01-24-2014 10:37 AM in reply to: Klingi162

Try the following Klingi:

 

(defun c:baksub ( / dir lst sub )
    (if
        (and
            (setq dir (vl-string-right-trim "\\" (vl-string-translate "/" "\\" (getvar 'dwgprefix)))
                  sub (strcat dir "\\bak")
                  lst (vl-directory-files dir "*.bak" 1)
            )
            (or (vl-file-directory-p sub)
                (vl-mkdir sub)
            )
            (setq lst
                (vl-remove nil
                    (mapcar
                       '(lambda ( bak )
                            (vl-file-rename
                                (strcat dir "\\" bak)
                                (strcat sub "\\" bak)
                            )
                        )
                        lst
                    )
                )
            )
        )
        (prompt (strcat "\n" (itoa (length lst)) " bak file(s) moved to " sub))
        (prompt "\nNo bak files found in working directory.")
    )
    (princ)
)
Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
*Expert Elite*
Lee_Mac
Posts: 1,038
Registered: ‎12-29-2009
Message 4 of 16 (335 Views)

Re: move bak files to project's subfolder

01-24-2014 10:41 AM in reply to: Lee_Mac

Or this may be better:

 

(defun c:baksub ( / dir lst sub )
    (if
        (and
            (setq dir (vl-string-right-trim "\\" (vl-string-translate "/" "\\" (getvar 'dwgprefix)))
                  sub (strcat dir "\\bak")
                  lst (vl-directory-files dir "*.bak" 1)
            )
            (or (vl-file-directory-p sub)
                (vl-mkdir sub)
            )
        )
        (prompt
            (strcat "\n"
                (itoa
                    (length
                        (vl-remove nil
                            (mapcar
                               '(lambda ( bak )
                                    (vl-file-rename
                                        (strcat dir "\\" bak)
                                        (strcat sub "\\" bak)
                                    )
                                )
                                lst
                            )
                        )
                    )
                )
                " of "
                (itoa (length lst))
                " bak file(s) moved to " sub
            )
        )
        (prompt "\nNo bak files found in working directory.")
    )
    (princ)
)
Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Member
Posts: 4
Registered: ‎01-24-2014
Message 5 of 16 (322 Views)

Re: move bak files to project's subfolder

01-24-2014 10:58 AM in reply to: gilsonrss
Replace function move_bak_file by

(defun move_bak_file (path / temp_name temp_path dir_bak)
(setq temp_name (vl-filename-base path))
(setq temp_path (vl-filename-directory path))
;Check to dir bak exists
(if (not (setq dir_bak (vl-directory-files temp_path "bak" -1)))
(if (not (vl-mkdir (strcat temp_path "\\bak")))
(progn
(alert "\nError: Error to creat dir bak")
(exit)
)
)
)
(setq dir_bak (strcat temp_path "\\bak\\"))
;Delete bak old
(vl-file-delete (strcat dir_bak temp_name))

(if (not (vl-file-rename path (strcat dir_bak temp_name ".bak")))
(progn
(alert "\nError: Error to move file bak")
(exit)
)
)
)
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 6 of 16 (296 Views)

Re: move bak files to project's subfolder

01-25-2014 04:55 AM in reply to: Lee_Mac

Hey Lee,

thanks for your prompt reply to my issue. works great...as usual. :smileywink:

Now I was wondering if there is also a way that it moves the .bak files every time I save the file without using any command? Is this even possible with lisps?

 

many thanks also to the other guys, I'm always impressed how many helpful people are on this platform. :smileyhappy:

 

*Expert Elite*
Lee_Mac
Posts: 1,038
Registered: ‎12-29-2009
Message 7 of 16 (294 Views)

Re: move bak files to project's subfolder

01-25-2014 05:01 AM in reply to: Klingi162

Klingi162 wrote:

Hey Lee,

thanks for your prompt reply to my issue. works great...as usual. :smileywink:


Excellent - you're welcome!


Klingi162 wrote:

Now I was wondering if there is also a way that it moves the .bak files every time I save the file without using any command? Is this even possible with lisps?


Yes, this is possile using a Command Reactor to evaluate the function following the SAVE / SAVEAS / QSAVE commands, however, rather than have a reactor running in the background to monitor every command invoked by the user, a far cleaner solution would be to simply evaluate the function from the acaddoc.lsp to clean up any bak files when a drawing is opened.

 

For this, just copy the code to your acaddoc.lsp and add:

 

(c:baksub)

 

After the last closing parenthesis.

 

Lee

 

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 8 of 16 (261 Views)

Re: move bak files to project's subfolder

01-25-2014 11:24 AM in reply to: Lee_Mac

I'm more and more impressed every day! I just tried it and it works great. :smileyhappy:

the only thing is, that I now manualy have to put it to every users acaddoc.lsp file, right?

or is there a way to change their default location to a directory on the server, if this is useful?

 

what do I have to do, if I want to put a row of comands to the "acaddoc.lsp" to load when opening a file, cause this would sometimes be useful to "force" the users computer to run certain commands. Is there a seperator between the comands or does it run them anyway if they are in this file?

 

many questions for a late saturday, hope your not to annoyed with all me requests. :smileyembarrassed:

 

cheers klingi

*Expert Elite*
Lee_Mac
Posts: 1,038
Registered: ‎12-29-2009
Message 9 of 16 (244 Views)

Re: move bak files to project's subfolder

01-25-2014 02:55 PM in reply to: Klingi162

Klingi162 wrote:

I'm more and more impressed every day! I just tried it and it works great. :smileyhappy:


 Excellent - that's great to hear Klingi :smileyhappy:


Klingi162 wrote:

the only thing is, that I now manualy have to put it to every users acaddoc.lsp file, right?

or is there a way to change their default location to a directory on the server, if this is useful?


If the acaddoc.lsp resides in a local Support File Search Path (henceforth referred to as 'SFSP'), then yes, you would need to create/modify the acaddoc.lsp for every user. However, if you were to add a new SFSP referencing a shared network path for every user, you could place the acaddoc.lsp in this network location for it to be loaded for every user.

 

Then, any modifications to the acaddoc.lsp would automatically be applied for every user with a SFSP pointing to the network location (assuming that the user does not have another acaddoc.lsp residing in a SFSP above the network location in the list of SFSPs).

 

I also discuss this in my post here.


Klingi162 wrote:

what do I have to do, if I want to put a row of comands to the "acaddoc.lsp" to load when opening a file, cause this would sometimes be useful to "force" the users computer to run certain commands. Is there a seperator between the comands or does it run them anyway if they are in this file?


The acaddoc.lsp is just like any other AutoLISP file - AutoCAD simply loads any LISP file found with the filename acaddoc.lsp, there is nothing special about the file format or content. Therefore, you would treat the content of the acaddoc.lsp like you would with any other LISP file.

 

Lee

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 10 of 16 (221 Views)

Re: move bak files to project's subfolder

01-26-2014 09:07 AM in reply to: Lee_Mac

Well, that sounds exactly what I was looking for. I also read your other posts about this issue and found it more than informative. especially the one about acaddoc.lsp  :smileyhappy:

Thanks for all the infos Lee. I will give it a try when I'm back in my office.

 

The only thing I'm still worried about, is the connecting of more than one lisp command in one file:

Do I have to use 

(c:comand_a)

 at the end of every code and 

(defun c:comand_b)

to start a new code in the same file? :smileyfrustrated:

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community