'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'DESCRIPTION: This script will clean up any files that have a last modified ' date greater than or equal to the number specified. 'CREATED BY: Brian Plexico - microISV.com 'MODIFIED BY: Gina Trapani (ginatrapani@gmail.com) 'MODIFIED BY: Peter Kolbus (prkolbus@unusualcode.com) 'CREATE DATE: 07/19/2005 'UPDATED: 08/13/2007 ' 'INSTRUCTIONS: 1. Enter the path to the directory you'd like to clean on line 28. ' Make sure to only change the text that currently reads: ' "C:\DIRECTORY_TO_CLEAN". The path must be surrounded by quotes. ' 2. Enter the number of days that must have passed for a file to be deleted ' on line 38. The default is 30 but can be changed to any number. ' 3. If you want to retain some of the subfolders so that they aren't deleted, ' you have two choices. See A & B below. ' A. Add the folder names you want to skip to the foldersToSkip line. ' Separate foldernames with a semicolon. ' Example: foldersToSkip = "Folder1;Folder2" ' B. Create a file named 'janitor.skip' in the folder(s) you want to ' leave alone. ' ' USAGE: {CScript|WScript} janitor.vbs [/D] [/F] [/A days] [/I path]... path_to_clean ' /D[ryrun] Don't actually delete files, just print their names. ' /F[orce] Delete files marked read-only. ' /A[ge] days Minimum age of files to delete, in days. ' /I[gnore] path Ignore an additional path. To ignore multiple paths, specify /I again. ' path_to_clean The directory to clean. ' ' 'LEGAL MUMBO JUMBO: -THIS SCRIPT HAS THE POTENTIAL TO BE VERY DAMAGING TO YOUR COMPUTER. ' -USE THIS SCRIPT AT YOUR OWN RISK. ' -I'M NOT RESPONSIBLE FOR ANY BAD EFFECTS OF THIS SCRIPT SUCH AS DIZZINESS, ' NOSE BLEEDS, OR A COMPLETELY DESTROYED OPERATING SYSTEM. ' -IF YOU HAVE DOUBTS AS TO WHAT YOU'RE DOING, THEN DON'T DO IT. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'DO NOT EDIT THE SECTION BELOW ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit On Error Resume Next Dim fso, PathToClean, numberOfDays, folder, rootFolder, objFolder, objSubfolders, objFiles, folderToClean, folderToCheck, fts, foldersToSkip, skippedfolders, DryRun, DeleteReadOnly Set fso = CreateObject("Scripting.FileSystemObject") Set fts = CreateObject("Scripting.Dictionary") 'DO NOT EDIT THE SECTION ABOVE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'ENTER THE PATH THAT CONTAINS THE FILES YOU WANT TO CLEAN UP '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Path to the root directory that you're cleaning up PathToClean = "PATH*TO*CLEAN*NOT*SPECIFIED" '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'ENTER THE NUMBER OF DAYS SINCE THE FILE WAS LAST MODIFIED ' 'ANY FILE WITH A DATE LAST MODIFIED THAT IS GREATER OR EQUAL TO 'THIS NUMBER WILL BE DELETED. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Specify the how many days old a file must be in order to be deleted. numberOfDays = 3 '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'ENTER THE NAMES OF THE FOLDERS YOU DO NOT WANT TO BE DELETED ' 'ALL FOLDERS WITH THE SPECIFIED NAME WILL NOT BE DELETED. 'ALL FILES IN THE SPECIFIED FOLDERS WILL NOT BE DELETED. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' INSTRUCTIONS TO ADD FOLDERS YOU WISH TO SKIP 'Add the names of the folders you wish to skip. 'Separate each name with a semicolon. 'If you don't wish to use this feature, the line below must look like this: foldersToSkip = "" 'The line must look like the following to skip folders: foldersToSkip = "Folder1;Folder2" foldersToSkip = "" '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'DON'T CHANGE ANYTHING BELOW THIS LINE ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' DryRun = False DeleteReadOnly = False ParseArguments 'Check to make sure path is not a drive root If Right(PathToClean, 2) = ":\" or Right(PathToClean, 1) = ":" Then WScript.Echo "Whoa Nelly! It's best not to run the Janitor on a drive root like " + PathToClean WScript.Quit End If '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Start at the folder specified and walk down the directory tree '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Set rootFolder = fso.GetFolder(PathToClean) If Err.Number > 0 Then WScript.Echo PathToClean + " is not a valid directory path (" + Err.Description + "). Please correct the path and run the script again." Wscript.Quit End If EnumerateFoldersToSkip(foldersToSkip) GetSubfolders(rootFolder) CleanupFiles(rootFolder) 'Let person know when the cleanup is complete If DryRun Then WScript.Echo "Above files are older than " + CStr(numberOfDays) + " days and would have been deleted from " + PathToClean Else WScript.Echo "Files older than " + CStr(numberOfDays) + " days have been deleted from " + PathToClean End If 'Clean up Set fso = Nothing Wscript.Quit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Sub ParseArguments() Dim args Dim i Set args = WScript.Arguments For i = 0 to args.Count - 1 If Left(UCase(args.Item(i)),2) = "/D" Then DryRun = true ElseIf Left(UCase(args.Item(i)),2) = "/F" Then DeleteReadOnly = True ElseIf Left(UCase(args.Item(i)),2) = "/A" Then i = i + 1 numberOfDays = CInt(args.Item(i)) ElseIf Left(UCase(args.Item(i)),2) = "/I" Then i = i + 1 fts.Add UCase(Trim(args.Item(i))), "" Else PathToClean = args.Item(i) End If Next End Sub Sub EnumerateFoldersToSkip(skippedfolders) If skippedfolders <> "" Then If InStr(1, skippedfolders, ";", 1) <> 0 Then Dim arrSkippedFolders, sf arrSkippedFolders = Split(skippedfolders, ";") For each sf In arrSkippedFolders fts.Add UCase(Trim(sf)), "" Next Else fts.Add UCase(skippedfolders), "" End If End If End Sub Sub GetSubfolders(folder) If CheckForSkip(folder) Then Exit Sub Dim oSubfolder Set objFolder = fso.GetFolder(folder) Set objSubfolders = objFolder.Subfolders For Each oSubfolder in objSubfolders WScript.Echo oSubfolder.Path If Not fts.Exists(UCase(oSubfolder.Name)) Then 'Recursively go down the directory tree GetSubfolders(oSubfolder) 'Cleanup any files that meet the criteria CleanupFiles(oSubfolder) 'Delete the folder if its empty CleanupFolder(oSubfolder) End If Next End Sub Sub CleanupFiles(folderToClean) If CheckForSkip(folderToClean) Then Exit Sub dim objFile Set objFolder = fso.GetFolder(folderToClean) Set objFiles = objFolder.Files For Each objFile in objFiles If DateDiff("d", objFile.DateLastModified, Now) > numberOfDays Then If DryRun Then WScript.Echo objFile.Path Else WScript.Echo "Deleting " + objFile.Path On Error Resume Next objFile.Delete(DeleteReadOnly) If fso.FileExists(objFile.Path) Then WScript.Echo "Could not delete: " + objFile.Path End If End If End If Next Set objFolder = Nothing Set objFiles = Nothing End Sub Sub CleanupFolder(folderToCheck) If CheckForSkip(folderToCheck) Then Exit Sub Set objFolder = fso.GetFolder(folderToCheck) Set objSubfolders = objFolder.Subfolders Set objFiles = objFolder.Files If objFiles.Count = 0 and objSubfolders.Count = 0 Then If DryRun Then WScript.Echo objFolder.Path Else objFolder.Delete(DeleteReadOnly) End If End If Set objFolder = Nothing Set objSubfolders = Nothing Set objFiles = Nothing End Sub Function CheckForSkip(folderToCheck) CheckForSkip = fso.FileExists(folderToCheck & "\JANITOR.SKIP") End Function