todouble22 Posted January 5, 2010 Posted January 5, 2010 I've written a batch file to delete backup files for our inactive projects I'm having problems with it going through the sub-folders and running. I was hoping someone could give some assistance or potentially a better way. I want to be able to do it outside of running autocad. This is what I have for the batch file: [for /r C:\TEST\"Working Drawings"\ %%f in (*.bak) do del C:\TEST\"Working Drawings"\*.bak] I appreciate any assistance. Quote
Lee Mac Posted January 5, 2010 Posted January 5, 2010 Hi Todd, Try this, but be careful. (defun c:delBak (/ Shell Fold Path File i) (vl-load-com) ;; Delete BAK Files ~ Lee Mac ~ 05.01.10 (setq *acad (cond (*acad) ((vlax-get-acad-object))) *doc (cond (*doc) ((vla-get-ActiveDocument *acad)))) (setq Shell (vla-getInterfaceObject *acad "Shell.Application") Fold (vlax-invoke-method Shell 'BrowseForFolder (vla-get-HWND *acad) "Select Directory" 0)) (vlax-release-object Shell) (if Fold (progn (setq i 0 Path (vlax-get-property (vlax-get-property Fold 'Self) 'Path)) (vlax-release-object Fold) (or (= "\\" (substr Path (strlen Path))) (setq Path (strcat Path "\\"))) (foreach file (mapcar (function (lambda (x) (strcat Path x))) (vl-directory-files Path "*.bak" 1)) (if (not (vl-catch-all-error-p (vl-catch-all-apply (function vl-file-delete) (list file)))) (setq i (1+ i)))) (princ (strcat "\n<< " (itoa i) " BAK Files Deleted >>"))) (princ "\n*Cancel*")) (princ)) Will only process current directory - not subfolders. Quote
todouble22 Posted January 5, 2010 Author Posted January 5, 2010 Hi Todd, Try this, but be careful. (defun c:delBak (/ Shell Fold Path File i) (vl-load-com) ;; Delete BAK Files ~ Lee Mac ~ 05.01.10 (setq *acad (cond (*acad) ((vlax-get-acad-object))) *doc (cond (*doc) ((vla-get-ActiveDocument *acad)))) (setq Shell (vla-getInterfaceObject *acad "Shell.Application") Fold (vlax-invoke-method Shell 'BrowseForFolder (vla-get-HWND *acad) "Select Directory" 0)) (vlax-release-object Shell) (if Fold (progn (setq i 0 Path (vlax-get-property (vlax-get-property Fold 'Self) 'Path)) (vlax-release-object Fold) (or (= "\\" (substr Path (strlen Path))) (setq Path (strcat Path "\\"))) (foreach file (mapcar (function (lambda (x) (strcat Path x))) (vl-directory-files Path "*.bak" 1)) (if (not (vl-catch-all-error-p (vl-catch-all-apply (function vl-file-delete) (list file)))) (setq i (1+ i)))) (princ (strcat "\n<< " (itoa i) " BAK Files Deleted >>"))) (princ "\n*Cancel*")) (princ)) Will only process current directory - not subfolders. Thanks LeeMac, but to run this I have to be in autocad correct and run the lisp routine? I copied a bunch of folders and sub-folders into another path string and tested my batch file by running this: DEL /S C:\TEST\"Working Drawings"\*.BAK I actually ran it from dos and it worked. I'm definately covering my a$$ to not run any of this on original files. thanks for your help as always.. Quote
ReMark Posted January 5, 2010 Posted January 5, 2010 Nice Lee but all that for deleting BAK files? In DOS that would be a one-liner. LOL Quote
Lee Mac Posted January 5, 2010 Posted January 5, 2010 Nice Lee but all that for deleting BAK files? In DOS that would be a one-liner. LOL True, but do you get a snazzy dialog in DOS... Quote
ReMark Posted January 5, 2010 Posted January 5, 2010 No, but I could get a confirmation switch if I want to. If I want snazzy dialog I just visit the chat area. Quote
todouble22 Posted January 5, 2010 Author Posted January 5, 2010 No, but I could get a confirmation switch if I want to. If I want snazzy dialog I just visit the chat area. How do you add a confirmation switch? /p? but does that do it for each file? ReMark is the way that I'm doing it the easiest fastest way? I thought I had seen a thread similar to this before but I couldn't find it? Just doing some housecleaning on old files. Quote
lpseifert Posted January 5, 2010 Posted January 5, 2010 How about a search for *.bak in Windows Explorer, Ctrl+A, Delete Quote
ReMark Posted January 5, 2010 Posted January 5, 2010 Yes, the /p switch would force a prompt after each file name is displayed. Press "Y" to confirm the deletion, "N" to cancel the deletion and display the next filename or "Ctrl+C" to stop the command. Is the way you're doing it the easiest, fastest way? I'm not sure. I suppose you could set it up as a .bat file and have it run everytime you boot up your computer. I clean out my bak files once a week prior to backing up all dwg files. Currently there aren't that many so it takes less than 30 seconds to find and delete the files. I do it via Windows Explorer. Quote
todouble22 Posted January 5, 2010 Author Posted January 5, 2010 Yes, the /p switch would force a prompt after each file name is displayed. Press "Y" to confirm the deletion, "N" to cancel the deletion and display the next filename or "Ctrl+C" to stop the command. Is the way you're doing it the easiest, fastest way? I'm not sure. I suppose you could set it up as a .bat file and have it run everytime you boot up your computer. I clean out my bak files once a week prior to backing up all dwg files. Currently there aren't that many so it takes less than 30 seconds to find and delete the files. I do it via Windows Explorer. Thats basically what I did I set the .bat file to run as a scheduled task , basically once a week. Quote
ReMark Posted January 5, 2010 Posted January 5, 2010 If it works then why are you messing with it? Your way is basically set-it and forget-it. I see nothing wrong with it. Quote
todouble22 Posted January 5, 2010 Author Posted January 5, 2010 If it works then why are you messing with it? Your way is basically set-it and forget-it. I see nothing wrong with it. "I have only come here seeking knowledge. Things they wouldn't teach me of in college." watch out for trademark infringement from the ronco corp for set it and forget it haha. I knew that it worked , I just figured with the knowledge base here I would see other users ways of tackling it. Quote
ReMark Posted January 5, 2010 Posted January 5, 2010 I think a lisp routine run from within AutoCAD would be overkill. I could however agree to a lisp routine run over a network to clean out all the bak files a group of networked CAD users generated in a day. It would be a highly efficient way of reducing clutter. Just my opinion. Quote
todouble22 Posted January 5, 2010 Author Posted January 5, 2010 I think a lisp routine run from within AutoCAD would be overkill. I could however agree to a lisp routine run over a network to clean out all the bak files a group of networked CAD users generated in a day. It would be a highly efficient way of reducing clutter. Just my opinion. That was exactly my goal in doing it. I was suprised that there was noting in place at the firm that I now work for. The practice before was going in and deleting them all one by one or searching through the files and sorting by type and deleting that way and I told them they were crazy. Quote
lpseifert Posted January 5, 2010 Posted January 5, 2010 There's Express Tools' Movebak command that saves the bak files to a designated directory, easy to delete from there. Quote
fixo Posted January 5, 2010 Posted January 5, 2010 I've written a batch file to delete backup files for our inactive projects I'm having problems with it going through the sub-folders and running. I was hoping someone could give some assistance or potentially a better way. I want to be able to do it outside of running autocad. This is what I have for the batch file:[for /r C:\TEST\"Working Drawings"\ %%f in (*.bak) do del C:\TEST\"Working Drawings"\*.bak] I appreciate any assistance. Paste this BAT file into drive you need Usualy I use D:\ drive on my machine @echo off rem Remove files with extension "BAK" del *.bak /s echo pause Working good on my Win XP I use the same way to delete ._LS and ._DC files too ~'J'~ Quote
todouble22 Posted January 5, 2010 Author Posted January 5, 2010 There's Express Tools' Movebak command that saves the bak files to a designated directory, easy to delete from there. thank you Paste this BAT file into drive you needUsualy I use D:\ drive on my machine @echo off rem Remove files with extension "BAK" del *.bak /s echo pause Working good on my Win XP I use the same way to delete ._LS and ._DC files too ~'J'~ ..and thank you as well Quote
CAB Posted January 6, 2010 Posted January 6, 2010 A bit of an overkill perhaps but this is mine. ;; del_files.lsp ;; CAB 10/09/2006 ;; Version 2.0 12.16.2009 added file report & replace folder dialog ;; ;; User selects top folder, allows subfolders, enters file types ;; File types matching are then deleted. ;; ;; (defun c:del_files (/ CNT CNTFAILED F OutFile FNAME FNAMELST FOLDER FTYPE FTYPES FULLPATH PATH PLISTS usesub X get_subs *error* ) ;;=============================================== ;; L o c a l F u n c t i o n s ;;=============================================== ;; error function & Routine Exit ;| (defun *error* (msg) (if (not (member msg '("console break" "Function cancelled" "quit / exit abort" "") ) ) (princ (strcat "\nError: " msg)) ) ; endif (if fso (vlax-release-object fso) ) (gc) (princ) ) |; ;; Return all directories (defun get_subs (folder / file) (mapcar '(lambda (x) (setq file (strcat folder "\\" x)) (cons file (apply 'append (get_subs file))) ) (cddr (vl-directory-files folder nil -1)) ) ) ;; FolderBox (Patrick_35) (defun FolderBox (message directory flag / folder sh) ;; Arguments: ;; message: the message displayed in th dialog box ;; directory: the directory to browse ;; flag values: ;; 0 = Default ;; 1 = Only file system folders can be selected. If this bit is set, the OK button is disabled if the user selects a folder that doesn't belong to the file system (such as the Control Panel folder). ;; 2 = The user is prohibited from browsing below the domain within a network (during a computer search). ;; 4 = Room for status text is provided under the text box. ;; 8 = Returns file system ancestors only. ;; 16 = Shows an edit box in the dialog box for the user to type the name of an item. ;; 32 = Validate the name typed in the edit box. ;; 512 = None "New folder" button ;; 4096 = Enables the user to browse the network branch of the shell's namespace for computer names. ;; 8192 = Enables the user to browse the network branch of the shell's namespace for printer names. ;; 16384 = Allows browsing for everything. (setq shell (vlax-create-object "Shell.Application")) (if (setq folder (vlax-invoke shell 'browseforfolder 0 message flag directory) ) (setq folder (vlax-get-property (vlax-get-property folder 'self) 'path)) (setq folder nil) ) (vlax-release-object shell) folder ) ;;=========================================================== ;; M a i n P r o g r a m ;;=========================================================== (setq path "" ; use current path ftypes "" ; show all types ) ;;Old method (setq fullpath (getfiled "Select a File in the top folder." path ftypes 4) ) (setq fullpath (FolderBox "Select top folder for File Delete:" (vl-filename-directory(findfile "acad.exe")) 0)) (if (and fullpath ;;Old method (setq path (vl-filename-directory fullpath)) ; extract path (setq path fullpath) ; new method ;;(setq fname (vl-filename-base fullpath)) ; extract name only ;;(setq ftype (vl-filename-extension fullpath)) ; extract file type ) (progn (initget "Yes No") (setq usesub (getkword "\nSearch in Subdirectories? [Yes/No] <No>")) (or usesub (setq usesub "No")) (initget "All bak bk1 bk2 sv$ ac$ log plt Ls Dc dS") (setq ftype (getkword "\nEnter File type [All bak bk1 bk2 sv$ ac$ log plt Ls Dc dS] <bak>")) (or ftype (setq ftype "bak")) (cond ((member ftype '("Ls" "Dc")) ; correct for missing _ character (setq ftype (list (strcat "_" ftype))) ) ((= ftype "All")(setq ftype '("bak" "bk1" "bk2" "sv$" "ac$" "log" "plt" "_Ls" "_Dc" "dS"))) (t (setq ftype (list ftype))) ) (prompt "\n *** Working - Please wait ***\n") (if (= usesub "Yes") (setq plists (cons (list path) (get_subs path))) ; use all subfolders too (setq plists (list path)) ; only current path ) (setq cnt 0 cntfailed 0 ) (if (setq OutFile (open (strcat path "\\FileDelList.txt") "w")) (progn (write-line (strcat (substr (rtos (getvar "CDATE") 2 4) 5 2) ; month "-" (substr (rtos (getvar "CDATE") 2 4) 7 2) ; day "-" (substr (rtos (getvar "CDATE") 2 4) 1 4) ; year "2007" ) OutFile) (write-line "=================================================" OutFile) (foreach pl plists (if (not (listp pl)) (setq pl (list pl))) (foreach pa pl (foreach ft ftype ;; get A LIST OF matching FILE NAMES (setq fnamelst (vl-directory-files pa (strcat "*." ft) 1)) (if fnamelst (foreach fn fnamelst (if (vl-file-delete (strcat pa "\\" fn)) (progn (write-line (strcat pa "\\" fn) OutFile) (setq cnt (1+ cnt)) ) (setq cntfailed (1+ cntfailed)) ) ) ) ) ) ) (write-line "=================================================" OutFile) (write-line (strcat (itoa cnt) " files were deleted.") OutFile) (write-line (strcat (itoa cntfailed) " files could NOT be deleted.") OutFile) (close OutFile) ; close the open file handle ) ) (if (not (zerop cnt)) (prompt (strcat "\nReport File --> " path "\\FileDelList.txt \n" (itoa cnt) " files were deleted."))) (if (not (zerop cntfailed)) (prompt (strcat "\n" (itoa cntfailed) " files could NOT be deleted.")) ) (if (and (zerop cnt) (zerop cntfailed)) (prompt (strcat "\n*** No matching files found ***.")) ) ) (prompt "\nUser Quit.") ) (princ) ) Quote
ReMark Posted January 6, 2010 Posted January 6, 2010 CAB: A bit of overkill? When you go hunting you must use a grenade launcher to zap a squirrel! LOL I think the use of the batch file (go DOS go!) is about as simple and effective as anything. Quote
Lee Mac Posted January 6, 2010 Posted January 6, 2010 Many thanks CAB, I was about to post a new thread over at theSwamp to get hold of these: ;; Arguments: ;; message: the message displayed in th dialog box ;; directory: the directory to browse ;; flag values: ;; 0 = Default ;; 1 = Only file system folders can be selected. If this bit is set, the OK button is disabled if the user selects a folder that doesn't belong to the file system (such as the Control Panel folder). ;; 2 = The user is prohibited from browsing below the domain within a network (during a computer search). ;; 4 = Room for status text is provided under the text box. ;; 8 = Returns file system ancestors only. ;; 16 = Shows an edit box in the dialog box for the user to type the name of an item. ;; 32 = Validate the name typed in the edit box. ;; 512 = None "New folder" button ;; 4096 = Enables the user to browse the network branch of the shell's namespace for computer names. ;; 8192 = Enables the user to browse the network branch of the shell's namespace for printer names. ;; 16384 = Allows browsing for everything. I couldn't understand the flags over at MSDN at all! Nice code BTW, I like the recursive folder retrieval! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.