cdtech9198
IS-IT--Management
I have an existing file that searchs a directory and its subfolders and will delete file based on date.
I would like to modify this script to include delete files based on a file name criteria. If a file name ends with _x.tif or _y.tif and older then 30 days then delete. Would it be in my best interest to use the RIGHT function or is there a better way to search the file name?
This is what I currently have. Can I simply add a "and" to this line "If file.DateLastModified < BeforeDate Then"
If file.DateLastModified < BeforeDate & (Right(file,6))= _x.tif or _y.tif Then"
Thank you.
I would like to modify this script to include delete files based on a file name criteria. If a file name ends with _x.tif or _y.tif and older then 30 days then delete. Would it be in my best interest to use the RIGHT function or is there a better way to search the file name?
This is what I currently have. Can I simply add a "and" to this line "If file.DateLastModified < BeforeDate Then"
If file.DateLastModified < BeforeDate & (Right(file,6))= _x.tif or _y.tif Then"
Thank you.
Code:
Dim fso, startFolder, OlderThanDate
set objShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "C:\tmp\" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -30, Date) ' 30 days
DeleteOldFiles startFolder, OlderThanDate
Function DeleteOldFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection, subFolder
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If file.DateLastModified < BeforeDate Then
fso.DeleteFile(file.Path)
End If
Next
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
Next
End Function