Option Explicit Dim debugMode, enableRecursion ' User configurable Dim intDaysOld, strLogFileSuffix ' User configurable Dim objPaths ' User configurable Dim curPath, deletedFiles, deleteFailures, freedSpace ' Managed by script Dim oFSO Set objPaths = WScript.CreateObject("Scripting.Dictionary") Set oFSO = CreateObject("Scripting.FileSystemObject") '======================================== '===== You can update these variables debugMode = 1 ' Set to 1 to not actually delete the files enableRecursion = 1 ' Set to 0 to disable, 1 to enable intDaysOld = 90 'Number of days to retain on the server strLogFileSuffix = ".log" 'The suffix of your log files objPaths.Add "Windows System LogFiles", "C:\WINDOWS\system32\LogFiles" objPaths.Add "Windows TEMP directory", "C:\WINDOWS\TEMP" '===== End user configurable '======================================== deletedFiles = 0 deleteFailures = 0 freedSpace = 0 Function isFolder(sFolderPath) Set oFSO = CreateObject("Scripting.FileSystemObject") isFolder = oFSO.FolderExists(sFolderPath) End Function Sub ProcessFolder(folder) Dim pf_folder, pf_subfolder, pf_subfolders, pf_file, pf_filename, pf_filesize Set pf_folder = oFSO.GetFolder(folder) ' Process the files in the folder WScript.Echo(" - Processing " & pf_folder.files.count & " files in " & folder) For Each pf_file in pf_folder.files pf_filename = pf_file.name pf_filesize = pf_file.size If datediff("d",pf_file.DateLastModified,Date()) > intDaysOld and lcase(right(pf_file.name,4))=strLogFileSuffix then If debugMode = 1 Then WScript.Echo(" -- Debug: Would have deleted " & pf_filename) deletedFiles = deletedFiles + 1 freedSpace = freedSpace + pf_filesize Else On Error Resume Next Err.Clear pf_file.delete If (Err.Number = 0) Then WScript.Echo(" -- " & pf_filename & " was deleted") deletedFiles = deletedFiles + 1 freedSpace = freedSpace + pf_filesize Else WScript.Echo(" -- Error " & Err.Number & ": failed to delete file " & pf_filename) deleteFailures = deleteFailures + 1 End If Err.Clear On Error GoTo 0 End If End If Next ' Process the subfolders in the current folder if recursion is enabled If enableRecursion > 0 AND pf_folder.SubFolders.count > 0 Then WScript.Echo(" * Processing subfolder(s) in " & folder) Set pf_subfolders = pf_folder.SubFolders For Each pf_subfolder in pf_subfolders ProcessFolder(folder & "\" & pf_subfolder.name) Next End If End Sub ' Make sure we're being run by cscrip If LCase(oFSO.GetFileName(WScript.FullName)) <> "cscript.exe" Then WScript.Echo("Please run this script using the command line and cscript.exe.") WScript.Quit End If WScript.Echo("Looking for files older than " & intDaysOld & " days") WScript.Echo("") For Each curPath in objPaths If isFolder(objPaths(curPath)) Then WScript.Echo("Processing " & objPaths(curPath)) ProcessFolder(objPaths(curPath)) WScript.Echo("...done!") WScript.Echo("") Else WScript.Echo("Error: The path " & objPaths(curPath) & " does not exist!") WScript.Echo("Exiting...") WScript.Quit End If Next WScript.Echo("Total deleted files: " & FormatNumber(deletedFiles)) WScript.Echo("Total delete failures: " & FormatNumber(deleteFailures)) WScript.Echo("Total freed space: " & FormatNumber(freedSpace) & " bytes")