PDA

View Full Version : Selecting a list of folders and removing all ".bak", ".err", ".xlg" & "plot.log" files from folders & sub folders


stephen.coff
2007-10-12, 08:36 AM
Guys,
Is it possible to be able to select multiple folders in a drive and have all ".bak", ".err", ".xlg" & "plot.log" files removed from not only those files but also any sub folders using lisp ?
Does anyone have anything like this they would share ?

Stephen

'gile'
2007-10-12, 11:44 AM
Hi,

This one doesn't allow to select multiple folders at once, but it clean up all the sub folders of the specified one.

;| GetFolders
;;; Returns the list of all sub folders of the specified folder or drive (path)
;;
;; Example
;; (GetFolders "C:\\Program Files\\AutoCAD 2007\\Help")
;; ("C:\\Program Files\\AutoCAD 2007\\Help\\buildyourworld"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\createTransmittal"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\crossReference"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\crossReference\\Models"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\PlaceView"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\PlaceView\\Models"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\PublishSheetSet"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\sheetListTable"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\Symbol Libraries"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\ViewSheetSet"
"C:\\Program Files\\AutoCAD 2007\\Help\\Tutorials\\ViewSheetSet\\xrefs")
|;

(defun GetFolders (path / l c)
(if (setq l (vl-directory-files path nil -1))
(apply 'append
(mapcar '(lambda (x)
(cons (setq c (strcat path "\\" x)) (GetFolders c))
)
(vl-remove "." (vl-remove ".." l))
)
)
)
)

;; str2lst
;; Parses a string in a sub string list
;;
;; Arguments
;; str : the string to be parsed
;; sep : the separator
;;
;; Examples
;; (str2lst "a b c" " ") -> ("a" "b" "c")
;; (str2lst "1,2,3" ",") -> ("1" "2" "3")
;; (mapcar 'read (str2lst "1,2,3" ",")) -> (1 2 3)

(defun str2lst (str sep / pos)
(if (setq pos (vl-string-position (ascii sep) str))
(cons (substr str 1 pos)
(str2lst (substr str (+ (strlen sep) pos 1)) sep)
)
(list str)
)
)

;; CleanFolder
;; Deletes all files with specified extensions in the specified folder and sub folders

(defun c:cleanfolder (/ path exts cnt)
(and
(or
(/= ""
(setq
path (getstring T "\nEnter the folder path <Dialog box>: ")
)
)
(setq path
(vl-filename-directory
(getfiled "Select a file in the folder to be cleaned"
""
""
0
)
)
)
)
(setq exts (getstring "\nEnter the extensions (bak,err,log,...): "))
(setq cnt 0)
(mapcar
'(lambda (fold)
(mapcar '(lambda (ext)
(mapcar '(lambda (file)
(vl-file-delete
(strcat fold "\\" file)
)
(setq cnt (1+ cnt))
)
(vl-directory-files fold (strcat "*." ext))
)
)
(str2lst exts ",")
)
)
(cons path (getfolders path))
)
)
(princ (strcat "\n\t" (itoa cnt) " files deleted"))
(princ)
)

bob.vandusen
2007-10-12, 01:47 PM
I run the following batch file nightly using scheduler to delete unwanted files prior to our scheduled tape backup.

del F:\*.gid /s /q /f
del F:\*.bak /s /q /f
del F:\*.dmp /s /q /f
del F:\*.bp3 /s /q /f
del F:\*.sv$ /s /q /f
del F:\*.err /s /q /f
del F:\*.bk1 /s /q /f

'gile'
2007-10-12, 05:48 PM
Here's a more convivial version, using dialog boxes
(comments aren't translated)

;;; GetFolders
;;; Retourne la liste de tous les sous-dossiers du dossier (ou disque) spécifié (chemin)

(defun GetFolders (path / l c)
(if (setq l (vl-directory-files path nil -1))
(apply 'append
(mapcar '(lambda (x)
(cons (setq c (strcat path "\\" x)) (GetFolders c))
)
(vl-remove "." (vl-remove ".." l))
)
)
)
)

;; str2lst
;; Transforme un chaine avec séparateur en liste de chaines
;;
;; Arguments
;; str : la chaine à transformer en liste
;; sep : le séparateur
;;
;; Exemples
;; (str2lst "a b c" " ") -> ("a" "b" "c")
;; (str2lst "1,2,3" ",") -> ("1" "2" "3")
;; (mapcar 'read (str2lst "1,2,3" ",")) -> (1 2 3)

(defun str2lst (str sep / pos)
(if (setq pos (vl-string-position (ascii sep) str))
(cons (substr str 1 pos)
(str2lst (substr str (+ (strlen sep) pos 1)) sep)
)
(list str)
)
)

;;; InputBox Ouvre une boite de dialogue pour récupérer une valeur (string)

(defun InputBox (Titre Message Defaut / *acad* users1 valeur)
(setq *acad* (vlax-get-acad-object)
users1 (getvar "users1")
)
(acad-push-dbmod)
(vla-eval *acad*
(strcat "ThisDrawing.SetVariable \"USERS1\","
"InputBox (\"" Message
"\", \"" Titre
"\", \"" Defaut
"\")"
)
)
(setq valeur (getvar "users1"))
(setvar "users1" users1)
(acad-pop-dbmod)
valeur
)

;;; MsgBox -Patrick_35-
;;; Ouvre une boite de dialogue pour récupérer la réponse à une question

(defun MsgBox (Titre Boutons Message Time / Reponse WshShell)
(setq WshShell (vlax-create-object "WScript.Shell"))
(setq Reponse (vlax-invoke
WshShell
'Popup
Message
Time
Titre
(itoa Boutons)
)
)
(vlax-release-object WshShell)
Reponse
)

;;; DirBox -Patrick_35-

(defun DirBox (Message Chemin Drapeau / rep sh)
(vl-load-com)
(setq sh (vlax-create-object "Shell.Application"))
(if (setq
rep (vlax-invoke sh 'browseforfolder 0 Message Drapeau Chemin)
)
(setq rep (vlax-get-property (vlax-get-property rep 'self) 'path))
(setq rep nil)
)
(vlax-release-object sh)
rep
)

;; CleanFolder (gile)
;; Supprime du dossier et de ses sous dossier tous les fichiers
;; aux formats spécifiées (séparateur = virgule).

(defun c:cleanfolder (/ path exts cnt)
(vl-load-com)
(and
(setq path (dirbox "Select the folder" "" 512))
(/= ""
(setq exts
(inputbox
"Cleanfolder"
"Enter the extensions of the files to be deleted
\n(separator = comma)"
""
)
)
)
(= 6
(msgbox
"CleanFolder"
52
(strcat "Do you realy want to delete all the files\n"
exts
"\nfrom the folder\n"
path
)
10
)
)
(setq cnt 0)
(mapcar
'(lambda (fold)
(mapcar
'(lambda (ext)
(mapcar '(lambda (file)
(vl-file-delete
(strcat fold "\\" file)
)
(setq cnt (1+ cnt))
)
(vl-directory-files
fold
(strcat "*."
(vl-string-left-trim
"."
(vl-string-left-trim "*" ext)
)
)
)
)
)
(str2lst exts ",")
)
)
(cons path (getfolders path))
)
(alert (strcat (itoa cnt) " deleted files"))
)
(princ)
)

FRAMEDNLV
2007-10-12, 06:21 PM
I run the following batch file nightly using scheduler to delete unwanted files prior to our scheduled tape backup.

del F:\*.gid /s /q /f
del F:\*.bak /s /q /f
del F:\*.dmp /s /q /f
del F:\*.bp3 /s /q /f
del F:\*.sv$ /s /q /f
del F:\*.err /s /q /f
del F:\*.bk1 /s /q /f

Thanks for the batch routine. I had one for creating a text file to show our *plt files.

If you want a list you can change it to:
Dir F:\*.gid *.bak *.dmp *.bp3 *.sv$ *.err *.bk1 /s /q > F_Drive_Junk.txt

Chris

bennybenny09
2009-04-17, 12:10 PM
hello everybody!
dossier de surendettement (http://www.dossierdesurendettement.org)