Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete file based on File name 1

Status
Not open for further replies.

cdtech9198

IS-IT--Management
Feb 14, 2008
48
US
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.
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
 
This syntax should work:
Code:
If file.DateLastModified < BeforeDate And _
   (LCase(Right(file.Name,6)) = "_x.tif" Or LCase(Right(file.Name,6)) = "_y.tif") Then

   ...

End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top