' DeleteOutdated ' ' Recursively deletes files, wich are older than specified, ' measured by modification date. Also deletes empty folders. ' Writes a logfile. ' ' Attributes ' MaxFileAge: Maximum file age in days (modification date) ' Path: Folder which contains the files ' ' VBScript reference: http://msdn.microsoft.com/en-us/library/t0aew7h6(VS.85).aspx ' FSO reference: http://msdn.microsoft.com/en-us/library/z9ty6h50(VS.85).aspx ' specify logile path (same path as script) Logfile = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)) & "deletelog.txt" ' allocate object memory Dim objFso, objLog ' create a FSO object Set objFso = CreateObject("Scripting.FileSystemObject") ' check if there is already a logfile If Not objFso.FileExists(Logfile) Then objFso.CreateTextFile(Logfile) End If ' open the Logfile Set objLog = objFso.OpenTextFile(Logfile, 8) ' 8 stands for append mode ' This is where the magic happens Sub DeleteOutdated(Path, MaxFileAge) Set objDir = objFso.GetFolder(Path) ' see if there are "old" files around and process them For Each objFile in objDir.Files If objFile.DateLastModified < (Date() - MaxFileAge) Then AskDelete = MsgBox("Delete " & objFile.Path & "?", 3, "Delete file") ' if "Yes" was pressed If AskDelete = 6 Then DeletedFile = objFile.Path ' cache filename of file to delete for log objFile.Delete(1) objLog.WriteLine Date() & ", " & Time() & " - Deleted file " & DeletedFile End If ' if "Cancel" was pressed If AskDelete = 2 Then ' close the Logfile objLog.Close ' abort script execution WScript.Quit End If End If Next ' subfolder processing (with recursion) For Each objSubfolder in objDir.Subfolders Call DeleteOutdated(objSubfolder, MaxFileAge) Next '---- comment following code if you don't need empty subfolder processing ' see if a folder is empty (no files and subfolders around) and process it If objDir.Files.Count = 0 And objDir.Subfolders.Count = 0 Then AskDelete = MsgBox("Delete empty folder " & objDir & "?", 3, "Delete empty folder") ' if "Yes" was pressed If AskDelete = 6 Then DeletedFolder = objDir ' cache filename of folder to delete for log objDir.Delete(1) objLog.WriteLine Date() & ", " & Time() & " - Deleted empty folder " & DeletedFolder End If ' if "Cancel" was pressed If AskDelete = 2 Then ' close the Logfile objLog.Close ' abort script execution WScript.Quit End If End If '---- End Sub ' call the Sub Call DeleteOutdated("C:\Temp", 30) ' close the Logfile objLog.Close