VBS Prune
From FrogspawnWiki
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.
' folder to start search in...path = "c:\Backups"' delete files older than 28 days...killdate = date() - 28
arFiles = Array()
set fso = createobject("scripting.filesystemobject")
' Don't do the delete while you still are looping through a' file collection returned from the File System Object (FSO).' The collection may get mixed up.' Create an array of the file objects to avoid this.'SelectFiles path, killdate, arFiles, truenDeleted = 0for n = 0 to ubound(arFiles)
'=================================================' Files deleted via FSO methods do *NOT* go to the recycle bin!!!'=================================================on error resume next 'in case of 'in use' files...
arFiles(n).delete true
if err.number <> 0 then
wscript.echo "Unable to delete: " & arFiles(n).path
elsenDeleted = nDeleted + 1end if
on error goto 0
nextmsgbox nDeleted & " of " & ubound(arFiles)+1 _
& " eligible files were deleted"sub SelectFiles(sPath,vKillDate,arFilesToKill,bIncludeSubFolders)
on error resume next
'select files to delete and add to array...'set folder = fso.getfolder(sPath)
set files = folder.files
for each file in files' uses error trapping around access to the' Date property just to be safe'dtlastmodified = null
on error resume Next
dtlastmodified = file.datelastmodifiedon error goto 0
if not isnull(dtlastmodified) Then
if dtlastmodified < vKillDate then
count = ubound(arFilesToKill) + 1
redim preserve arFilesToKill(count)
set arFilesToKill(count) = file
end if
end if
nextif bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vKillDate,arFilesToKill,true
nextend if
end sub