This is a handy little disk maintenance script I put together to delete files older then x days.
It was written to delete log and text files that custom data feeds produce and which previously had to be deleted manually.
It needs to be saved with a .vbs extension (visual basic scripting). Just add a call to DeleteOldFiles for each directory and set it up under Scheduled tasks.
If you use it, find it useful or have any suggestions/bugs then please leave a comment to let me know. Thanks Gerard
'Gerard Conlon May 2008
'This VB script deletes files older then a certain date in a specifed directory
'The following call will delete all .log and .txt files in the c:\test dir older then 30 days
DeleteOldFiles "c:\test",30, ".log .txt"
'#####################################################
Sub DeleteOldFiles(sDir, iDaysOldToDelete, sExts)
'sDir is the drive and path to the dir for the files to be deleted
'iDaysOldToDelete is the number of days back to delete
'sExts is file extensions to delete - only 3 char extensions preceeded by the dot
Dim oFSO, oFolder, oFiles, oFile
Dim arrSplitName, strExtension
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDir)
Set oFiles = oFolder.Files
For Each oFile In oFiles
'check if it is one of the extensions passed in
If InStr(sExts, LCase(Right(oFile.Name, 4))) Then
'if the last modified date + the age period is less then now then delete the file
If DateAdd("d", iDaysOldToDelete, oFile.DateLastModified) < ofiles =" Nothing" ofolder =" Nothing" ofso =" Nothing">