VBS Prune

From FrogspawnWiki

Jump to: navigation, search

Preamble

I have need to use a Windows server as a back up server. All my kit drops backups onto the server every day using FTP. The trouble is that if left unchecked, these backup files would eventually fill the hard drive of the server and make the tape backup job so much longer. Therefore it is necessary to delete files after a couple of weeks.

In Linux (where I'm more comfortable) you could use a single find command to do this, however Windows doesn't have that available. It does have VB Script available though, and the following script accomplishes the task.

The Script

This script will locate any files older than the number of days specified in killdate (line 6) in or below the directory specified in path (line 2). You probably want to create a scheduled task to run it every day.


  1. ' folder to start search in... 
  2. path = "c:\Backups" 
  3.  
  4.  
  5. ' delete files older than 28 days... 
  6. killdate = date() - 28 
  7.  
  8.  
  9. arFiles = Array() 
  10. set fso = createobject("scripting.filesystemobject") 
  11.  
  12.  
  13. ' Don't do the delete while you still are looping through a 
  14. ' file collection returned from the File System Object (FSO). 
  15. ' The collection may get mixed up. 
  16. ' Create an array of the file objects to avoid this. 
  17. ' 
  18. SelectFiles path, killdate, arFiles, true 
  19.  
  20.  
  21. nDeleted = 0 
  22. for n = 0 to ubound(arFiles) 
  23.   '================================================= 
  24.   ' Files deleted via FSO methods do *NOT* go to the recycle bin!!! 
  25.   '================================================= 
  26.   on error resume next 'in case of 'in use' files... 
  27.   arFiles(n).delete true 
  28.   if err.number <> 0 then 
  29.     wscript.echo "Unable to delete: " & arFiles(n).path 
  30.   else 
  31.     nDeleted = nDeleted + 1 
  32.   end if 
  33.   on error goto 0 
  34. next 
  35.  
  36.  
  37. msgbox nDeleted & " of " & ubound(arFiles)+1 _ 
  38.   & " eligible files were deleted" 
  39.  
  40.  
  41. sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders) 
  42.   on error resume next 
  43.   'select files to delete and add to array... 
  44.   ' 
  45.   set folder = fso.getfolder(sPath) 
  46.   set files = folder.files 
  47.  
  48.  
  49.   for each file in files 
  50.     ' uses error trapping around access to the 
  51.     ' Date property just to be safe 
  52.     ' 
  53.     dtlastmodified = null 
  54.     on error resume Next 
  55.     dtlastmodified = file.datelastmodified 
  56.     on error goto 0 
  57.     if not isnull(dtlastmodified) Then 
  58.       if dtlastmodified < vKillDate then 
  59.         count = ubound(arFilesToKill) + 1 
  60.         redim preserve arFilesToKill(count) 
  61.         set arFilesToKill(count) = file 
  62.       end if 
  63.     end if 
  64.   next 
  65.  
  66.  
  67.   if bIncludeSubFolders then 
  68.     for each fldr in folder.subfolders 
  69.       SelectFiles fldr.path,vKillDate,arFilesToKill,true 
  70.     next 
  71.   end if 
  72. end sub
Personal tools