Delete oldest file in folder

Delete oldest file in folder

dvertz
Collaborator Collaborator
411 Views
5 Replies
Message 1 of 6

Delete oldest file in folder

dvertz
Collaborator
Collaborator

I have been trying to work out out how to delete the oldest file in a directory of files. This is what I came up with and wanted to see if anyone has a better idea. This can perform rather slow when the directory has nearly 5000 files.

 

 (setq test (vl-directory-files "C:\\Autocad\\temp" "*.*" 1))
 (while (> (length test) 1)
   (if (>
         (vl-file-systime (strcat "C:\\Autocad\\temp\\" (nth 0 test)))
         (vl-file-systime (strcat "C:\\Autocad\\temp\\" (nth 1 test)))
       )
     (setq test (cdr test))
     (setq test (append (list (car test)) (cddr test)))
   )
 )
 (vl-file-delete (car test))

 

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Accepted solutions (1)
412 Views
5 Replies
Replies (5)
Message 2 of 6

rkmcswain
Mentor
Mentor

Does it have to be autolisp? There are lots of examples of doing this in powershell out there.

R.K. McSwain     | CADpanacea | on twitter
0 Likes
Message 3 of 6

dvertz
Collaborator
Collaborator

I believe it does, but cannot be sure. I am not familiar with how to code for powershell. And furthermore, this is being ran from inside a reactor. Can it be called to perform the powershell script from inside the reactor?

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2


0 Likes
Message 4 of 6

ВeekeeCZ
Consultant
Consultant

Try this if it's any faster.

 

(vl-load-com)
(defun c:DelOldestFile ( / :sysdatesort pth lst fil) (defun :sysdatesort (l n) (setq l (vl-sort l '(lambda (e1 e2) (< (nth n e1) (nth n e2)))) l (vl-remove-if-not '(lambda (x) (= (nth n (car l)) (nth n x))) l)) (if (or (= n 8) (= (length l) 1)) (car l) (:sysdatesort l (1+ n)))) (and (setq pth "C:\\Autocad\\temp\\") (setq lst (vl-directory-files pth "*.*" 1)) (setq lst (mapcar '(lambda (x) (cons x (vl-file-systime (strcat pth x)))) lst)) (setq fil (strcat pth (car (:sysdatesort lst 1)))) (vl-file-delete fil) (princ (strcat fil " file has been deleted.")) ) (princ) )

 

0 Likes
Message 5 of 6

Moshe-A
Mentor
Mentor

@dvertz  hi,

 

Heres a function (get-earliest-file) to return the earliest file on specified folder. it's a list of dotted pair

(("filename" . <sys-filetime>))

the return time is base on dosLib (google for it) (dos_filedate) function which return Julian time format (similar to  CDATE sysvar) the list of files is sorted base on time from the smallest to biggest whereas the older file will be the first item in list.

 

cheers

Moshe

 

(defun get-earliest-file (/ FPATH)
 (setq FPATH "C:\\Autocad\\temp\\") 

 (car
   (vl-sort 
    (mapcar
     '(lambda (file)
       (dos_filedate (strcat FPATH file))
      )
     (vl-directory-files FPATH "*.*" 1)
    )
    (function (lambda (e1 e2) (< (cdr e1) (cdr e2))))  
   )
 )
)

 

 

0 Likes
Message 6 of 6

dvertz
Collaborator
Collaborator
Accepted solution

Thank you all for the suggestions. I really appreciate the help.

 

During my test of each one of the suggestions (including my own) I found where the slowness was coming from. I tend to test each line with copy past into Autocad while I am working out the bugs. The slow down was actually during the "vl-directory-files" sending the results to the command line output. Once I wrapped my code in the "defun" it was only slightly slower than beekeeCZ's code. And not slow enough to be of concern.

Civil 3D 2022,
Windows 10 Pro, x64, Nvidia Quadro P1000
Intel Core i9-11900k; 3.50GHz, 32 GB RAM, 500GB WD BLACK M.2