Deleteing Files more than xx Day’s Old

By | 4 March, 2009

Ever wanted to remove log files on a regular basis, say anything older than 30 days.  Well here’s a nice little VBScript that when run as a schedualed task can do it for you.  The first will look at the date created and compare it to the current data, the second will look at the last modified date.

-=-=-=-=-=-=-=-

‘ DelBasedOnCreationDate.vbs – delete files created over 30 days ago based on Date Created
Set fs = CreateObject(“Scripting.FileSystemObject”)
for each f in fs.getfolder(“c:\test”).Files
if Datediff(“d”,date,f.DateCreated) < -30 then
wscript.echo f.DateCreated & ”     ” & f.name
‘f.delete
end if
next

-=-=-=-=-=-=-=-

‘ DelBasedOnLastModifiedDate.vbs – delete files created over 30 days ago based on Date Modified
Set fs = CreateObject(“Scripting.FileSystemObject”)
for each f in fs.getfolder(“c:\test”).Files
if Datediff(“d”,date,f.DateLastModified) < -30 then
wscript.echo f.DateLastModified & ”     ” & f.name
‘f.delete
end if
next

Leave a Reply