Author Topic: Delete backup files more than a certain age  (Read 2969 times)

0 Members and 1 Guest are viewing this topic.

Chris

  • Swamp Rat
  • Posts: 548
Delete backup files more than a certain age
« on: August 27, 2009, 09:40:28 AM »
Currently I have an autosave program running that creates a drawing backup every 10 minutes and saves it in a folder on my hard drive. I would like to get a program that will delete all files automatically that are more than a certain age. I would like it to run automatically at a particular time and delete all files that are older than the previous month (if it ran today it would delete all files older than July 1, if it ran on September 1, it would delete all files older than August 1) Any suggestions?
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Delete backup files more than a certain age
« Reply #1 on: August 27, 2009, 09:53:18 AM »
Yes, I think this could be achieved, but you'd have to be careful  :wink:

Look into the functions: vl-file-delete and vl-file-systime

Lee

CHulse

  • Swamp Rat
  • Posts: 504
Re: Delete backup files more than a certain age
« Reply #2 on: August 27, 2009, 09:55:00 AM »
I would think you could do that with a simple batch file and set it up as a scheduled task...
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

Chris

  • Swamp Rat
  • Posts: 548
Re: Delete backup files more than a certain age
« Reply #3 on: August 27, 2009, 09:55:52 AM »
How would I create a list of the files in a folder so I could pass them to a while loop, or foreach that would delete the file if the systime is older than the specified date?

I could indeed use a batch file, I am already using one that will copy fonts to a user's fonts folder, but if I chose that method, how would I run it silently, I really only know how to run command prompt batch files and the command window popping up all the time may get a little annoying for some users.
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Delete backup files more than a certain age
« Reply #4 on: August 27, 2009, 10:05:19 AM »
also look at (dos_filedate <fileName>)

Code: [Select]
(vl-directory-files dir pat dirs) ; recognizes wildcards - CAB only "L*.dwg" basic letters not real wildcards
(vl-directory-files  [directory pattern directories])

directory
A string naming the directory to collect files for; if nil or absent, vl-directory-files uses the current directory.

pattern
A string containing a DOS pattern for the file name; if nil or absent, vl-directory-files assumes "*.*"

directories
 An integer that indicates whether the returned list should include directory names. Specify one of the following:
-1  List directories only.
0   List files and directories (the default).
1   List files only.

Return Values
A list of file and path names, or nil, if no files match the specified pattern.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CHulse

  • Swamp Rat
  • Posts: 504
Re: Delete backup files more than a certain age
« Reply #5 on: August 27, 2009, 10:10:21 AM »
...
I could indeed use a batch file, I am already using one that will copy fonts to a user's fonts folder, but if I chose that method, how would I run it silently, I really only know how to run command prompt batch files and the command window popping up all the time may get a little annoying for some users.

Ahh, that's true. I don't know how you could make a batch run silent...
Cary Hulse
Urban Forestry Manager
Wetland Studies and Solutions

Civil 3D 2020 & 2023

Chris

  • Swamp Rat
  • Posts: 548
Re: Delete backup files more than a certain age
« Reply #6 on: August 27, 2009, 10:12:06 AM »
...
I could indeed use a batch file, I am already using one that will copy fonts to a user's fonts folder, but if I chose that method, how would I run it silently, I really only know how to run command prompt batch files and the command window popping up all the time may get a little annoying for some users.

Ahh, that's true. I don't know how you could make a batch run silent...
maybe not silent, but minimized?
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10

Chris

  • Swamp Rat
  • Posts: 548
Re: Delete backup files more than a certain age
« Reply #7 on: August 27, 2009, 10:16:53 AM »
also look at (dos_filedate <fileName>)

Code: [Select]
(vl-directory-files dir pat dirs) ; recognizes wildcards - CAB only "L*.dwg" basic letters not real wildcards
(vl-directory-files  [directory pattern directories])

directory
A string naming the directory to collect files for; if nil or absent, vl-directory-files uses the current directory.

pattern
A string containing a DOS pattern for the file name; if nil or absent, vl-directory-files assumes "*.*"

directories
 An integer that indicates whether the returned list should include directory names. Specify one of the following:
-1  List directories only.
0   List files and directories (the default).
1   List files only.

Return Values
A list of file and path names, or nil, if no files match the specified pattern.
I think it gets to a certain length and then truncates the rest of the output.  Whenever the program is run, the folder will probably contain between 1000-2000 dwg files.
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Delete backup files more than a certain age
« Reply #8 on: August 27, 2009, 10:25:11 AM »
I don't have any folders like that to test but you could loop the function feeding it one letter at at time.
Code: [Select]
Loop
(setq fnames (append (vl-directory-files dir (strcat First_letter "*.dwg")  dirs) fnames))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Delete backup files more than a certain age
« Reply #9 on: August 27, 2009, 10:40:16 AM »
I really only know how to run command prompt batch files and the command window popping up all the time may get a little annoying for some users.
So this is something you're going to deploy to all users?
TheSwamp.org  (serving the CAD community since 2003)

Chris

  • Swamp Rat
  • Posts: 548
Re: Delete backup files more than a certain age
« Reply #10 on: August 27, 2009, 10:42:59 AM »
yes, this is something that I plan on deploying to all users.  AutoCAD's autosave feature doenst really work when you need it, so I plan on giving this autosave program to everyone. I just need to set it up so they dont need to be concerned about their harddrive filling up.
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Delete backup files more than a certain age
« Reply #11 on: August 27, 2009, 10:55:41 AM »
...
I could indeed use a batch file, I am already using one that will copy fonts to a user's fonts folder, but if I chose that method, how would I run it silently, I really only know how to run command prompt batch files and the command window popping up all the time may get a little annoying for some users.

Ahh, that's true. I don't know how you could make a batch run silent...
See Help: Start and Exit
TheSwamp.org  (serving the CAD community since 2003)

Chris

  • Swamp Rat
  • Posts: 548
Re: Delete backup files more than a certain age
« Reply #12 on: September 03, 2009, 09:36:02 AM »
to solve my issue, I created a fake file that gets updated every day, so it always has the current date for the vl-file-systime then I compare the cadr of the list to the cadr of all the other items in the folder (cadr is the month number) and if the month number is >= 2 the file gets deleted.  I put all the files in a folder into a list using
Code: [Select]
(setq
      dwglst (mapcar
       '(lambda (x) (strcat "C:/autocad log files/" x))
       (vl-directory-files "C:/autocad log files/" "*.log" 1)
     ) ;_ end of mapcar
    )
and then use foreach to filter each drawing.
I would post my code, but it would need a lot of revising for it to work for anyone else, I have a lot of office specific paths and files specified.
Christopher T. Cowgill, P.E.
AEC Collection 2020 (C3D)
Win 10