This VBScript will delete all files older than 3 months in a specified folder and all subfolders. It will also create a log file with all processed folders and deleted files.
Download
‘ Folder path
const fldname = “\\your\folder\path”
set fso = createobject(“scripting.filesystemobject”)
set fldr = fso.getfolder(fldname)
dttoday = date
tmnow = Time
‘Calculate trigger date (change according to your requirements)
dtold = dateadd(“m”, -3, dttoday) ‘files older than 3 months will be deleted
‘DateAdd(interval,number,date)
‘Interval:
‘ * yyyy – Year
‘ * q – Quarter
‘ * m – Month
‘ * y – Day of year
‘ * d – Day
‘ * w – Weekday
‘ * ww – Week of year
‘ * h – Hour
‘ * n – Minute
‘ * s – Second
‘ Log File
Set LogFSO = CreateObject(“Scripting.FileSystemObject”)
FileName = Year(dttoday)&”.”&Month(dttoday)&”.”&Day(dttoday)&”-“&Hour(tmnow)&”.”&Minute(tmnow)&”.”&Second(tmnow)&”.txt”
Set LogFile = LogFSO.OpenTextFile(FileName, 2, True)
recurse fldr
sub recurse( byref fldr)
dim subfolders,files,folder,file
set subfolders = fldr.subfolders
set files = fldr.files
LogFile.WriteLine(fldr.path)
for each file in files
if file.datelastmodified < dtold then
LogFile.WriteLine(” 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
Leave a Reply