Delete Files/Folders older than specified date
””””””””””””””””””””””””””””””””””””””””””””””””’
‘Created by Mark Torng (NTServices”
‘Created October 17th 2007
‘Some Lines Adopted from Microsoft Scripting Center ””””””””””””””””””””””””””””””””””””””””””””””””’
Option Explicit
On Error Resume Next
Dim fso, PathToClean, numberOfDays, folder, rootFolder, objFolder, objSubfolders, objFiles, folderToClean, folderToCheck, fts, foldersToSkip, skippedfolders Set fso = CreateObject(“Scripting.FileSystemObject”)
Set fts = CreateObject(“Scripting.Dictionary”)
‘ENTER THE PATH THAT CONTAINS THE FILES YOU WANT TO CLEAN UP
‘Path to the root directory that you’re cleaning up PathToClean = “C:\THIS FOLDER”
‘ENTER THE NUMBER OF DAYS SINCE THE FILE WAS LAST MODIFIED
numberOfDays = 1
‘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 ‘If you don’t want to save anything use: foldersToSkip = “”
‘If you want to omit folders: foldersToSkip = “Folder1;Folder2″
foldersToSkip = “”
‘Check to make sure path is not a drive root If Right(PathToClean, 2) = “:\” or Right(PathToClean, 1) = “:” Then
msgbox “Whoa Nelly! It’s best not to run the Janitor on a drive root like ” + PathToClean, vbOkOnly, “Don’t Do That!”
End If
‘Start at the folder specified and walk down the directory tree
Set rootFolder = fso.GetFolder(PathToClean) If Err.Number > 0 Then
msgbox PathToClean + “is not a valid directory path. Please correct the path and run the script again.”, vbOkOnly, “Path Not Found”
Wscript.Quit
End If
EnumerateFoldersToSkip(foldersToSkip)
GetSubfolders(rootFolder)
CleanupFiles(rootFolder)
Set fso = Nothing
Wscript.Quit
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
If fts.Exists(UCase(oSubfolder.Name)) = False Then
‘Recursively go down the directory tree
GetSubfolders(oSubfolder.Path)
‘Cleanup any files that meet the criteria
CleanupFiles(oSubfolder.Path)
‘Delete the folder if its empty
CleanupFolder(oSubfolder.Path)
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
objFile.Delete
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
objFolder.Delete
End If
Set objFolder = Nothing
Set objSubfolders = Nothing
Set objFiles = Nothing
End Sub
Function CheckForSkip(folderToCheck)
CheckForSkip = fso.FileExists(folderToCheck & “\JANITOR.SKIP”) End Function


