Home » .NET 4.0 » Delete old images – VB Script

Delete old images – VB Script

Copy this code into notepad and save as DeleteOldImages.vbs


'******************* Start of Code *************************

Option Explicit 
On Error Resume Next 
Dim oFSO, oFolder, sDirectoryPath  
Dim oFileCollection, oFile, sDir  
Dim iDaysOld 

' Specify Directory Path From Where You want to clear the old files 

sDirectoryPath = "C:\Users\mike\Desktop\Test" 

' Specify Number of Days Old File to Delete

iDaysOld = 15

Set oFSO = CreateObject("Scripting.FileSystemObject") 
Set oFolder = oFSO.GetFolder(sDirectoryPath) 
Set oFileCollection = oFolder.Files 

For each oFile in oFileCollection

'This section will filter the text file as i have used for for test 
'Specify the Extension of file that you want to delete 
'and the number with Number of character in the file extension  
	
  If LCase(Right(Cstr(oFile.Name), 3)) = "png" 
     OR LCase(Right(Cstr(oFile.Name), 3)) = "gif"  
     OR LCase(Right(Cstr(oFile.Name), 3)) = "jpg" Then
 	
 	If oFile.DateLastModified < (Date() - iDaysOld) Then 
           oFile.Delete(True) 
        End If 

	End If     
Next 

Set oFSO = Nothing 
Set oFolder = Nothing 
Set oFileCollection = Nothing 
Set oFile = Nothing 

'************************ End of Code ********************


Credit for this script - "Sidd"
http://www.symantec.com/connect/downloads/vbscript-delete-old-files