Wednesday, June 8, 2005

VBScript to delete all files older than 2 months

const fldname = "c:\test"
set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder(fldname)
set dttoday = date
set dt2monthsago = dateadd("m", -2, dttoday)

recurse fldr

sub recurse( byref fldr)
dim subfolders,files,folder,file
set subfolders = fldr.subfolders
set files = fldr.files
wscript.echo fldr.path

for each file in files
if file.datelastmodified < dt2monthsago then
wscript.echo "Deleting " & file.name & " last modified: " & file.datelastmodified
on error resume next
file.delete
end if

next
for each folder in subfolders
recurse folder
next
set subfolders = nothing
set files = nothing
end sub

No comments:

 
Jeffrey Ting Jeffrey Ting on Facebook Jeffrey Ting on Spock Jeffrey Ting on Plaxo Jeffrey Ting on Spoke Jeffrey Ting on LinkedIn