There are files that go back many years. The files are under a drive with 2 "Main" sub directories (test1 and test2). The test1 and test2 folders have thousands of subfolders with a named date format IE 20010329. Said example subfolder has numerous files and subfolders. I am attempting to create a Script to Delete files/folders older than 7 years to date by creation date. Limits: to not delete "files" in the 2 main directories (test1/test2) but any subfolder with files with created date beyond the 7 yr mark are fair game. Below is a script found and modified (sorry I do not know the creator) with correct/working date function but I cannot get the search folders right. I have attempted to take the script very close to the original state.
' Folder path
const fldname = "Z:\"
set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder(fldname)
dttoday = date
tmnow = Time
'Calculate trigger date (change according to your requirements)
dtold = dateadd("yyyy", -7, dttoday) 'files older than 7 years 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.datecreated < dtold then
LogFile.WriteLine(" DELETING - " & file.name & " Date Created: " & file.datecreated)
on error resume next
'file.delete
WScript.Echo File
end if
next
for each folder in subfolders
recurse folder
next
set subfolders = nothing
set files = nothing
end sub
' Folder path
const fldname = "Z:\"
set fso = createobject("scripting.filesystemobject")
set fldr = fso.getfolder(fldname)
dttoday = date
tmnow = Time
'Calculate trigger date (change according to your requirements)
dtold = dateadd("yyyy", -7, dttoday) 'files older than 7 years 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.datecreated < dtold then
LogFile.WriteLine(" DELETING - " & file.name & " Date Created: " & file.datecreated)
on error resume next
'file.delete
WScript.Echo File
end if
next
for each folder in subfolders
recurse folder
next
set subfolders = nothing
set files = nothing
end sub