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

Delete files older that 'X' days in specific folder

4 REPLIES 4
Reply
Message 1 of 5
jpCADconsulting
190 Views, 4 Replies

Delete files older that 'X' days in specific folder

Hi folks. I had some LISP in acad.doc that would delete all files in "C:\_AutoSave" that are older than 180 days on launch of AutoCAD.

 

It used to work -- I think. Here is the code. It just reports 'nil' and does nothing.

 

Any light you can shine on this for me is greatly appreciated.

 

(foreach file
           (mapcar (function (lambda (x) (strcat "C:/_AutoSave/" x)))
                   (vl-directory-files "C:/_AutoSave/" "*.*" 1)
           )
    (if (not (vl-file-systime file))
      (prompt (strcat "\n** No systime for \"" file "\" ** "))
    )
  )

 

4 REPLIES 4
Message 2 of 5
pbejse
in reply to: jpCADconsulting

 

This line doesn't do anything except evaluate if the variable file does not evaluate to nil

...
(if (not (vl-file-systime file))
      (prompt (strcat "\n** No systime for \"" file "\" ** "))
    )
...

There's something missing in the code you posted @jpCADconsulting 

 

Message 3 of 5

modify the code here to suit your needs

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 4 of 5
Moshe-A
in reply to: jpCADconsulting

@jpCADconsulting  hi,

 

here is a fix to your code

 

to find the difference between dates i assumed :

1 year contains 365 days in average

1  month contains 30 days in average 

 

to calculate the exact days is another 'story' :grinning_face:

 

enjoy

Moshe

 

(defun delete_older_files (/ elapses_days ; local function
			     fname f sysTime^ fileTime^ days0 days1 file)

 ; anonymous function
 (setq elapsed_days (lambda (lst) (+ (* (car lst) 365) (* (cadr lst) 30) (cadddr lst))))

       
 (setq fname (vl-filename-mktemp)) ; temporay file name
 (setq f (open fname "w"))
 (setq f (close f))

 (setq sysTime^ (vl-file-systime fname))
 (setq days0 (elapsed_days sysTime^))
  
 (foreach file (mapcar (function (lambda (x) (strcat "C:/_AutoSave/" x)))
		      					(vl-directory-files "C:/_AutoSave/" "*.*" 1))
  
  (if (not (setq fileTime^ (vl-file-systime file)))
   (prompt (strcat "\n** No systime for \"" file "\" ** "))
   (progn
    (setq days1 (elapsed_days fileTime^))

    (if (> (- days0 days1) 180)
     (vl-file-delete file)
    )
   ); progn
  ); if
 ); foreach
); delete_older_files

 

 

Message 5 of 5
jpCADconsulting
in reply to: pbejse

Yeah, I didn't know why I didn't see that. Totally obvious now. :winking_face:

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report