Submitted By: Vedran Matica

This script deletes files (for example IIS log files) older than specific number of days. The number of days is contained in a variable named intOlderThan, and the default value is 90 days. The script can delete files from multiple locations; an array named arrFolders contains the paths to the folders.

Visual Basic
Edit|Remove
On Error Resume Next

'The following variabe contains number of days. Default is 90.
intOlderThan = 90

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

'The following array contains path to folder(s) which contain IIS log files. 
'You can enter multiple folders.
arrFolders = Array("C:\WINDOWS\system32\Logfiles\W3SVC1")

For Each strPath in arrFolders
    Set objFolder = objFSO.GetFolder(Chr34&strPath&Chr34)
    Set colFiles = objFolder.Files
    For Each objFile In colFiles
        dateModified = objFile.DateLastModified
        intDaysOld = DateDiff("d", dateModified, Now)
        If intDaysOld > intOlderThan Then
            objFile.Delete
        End If
    Next
    ErrorCheck ()
Next


'Function for error checking.
Function ErrorCheck()
    If Err.Number <> 0 Then
        WshShell.LogEvent 1, "Script encountered error. Error number: " & Err.Number & _
            ". Error description: " & Err.Description
        WScript.Quit
    Else
        WshShell.LogEvent 4, "Script completed successfully."		
    End If
End Function