Skip to content

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

5 Comments Post a comment
  1. In the awesome design of things you’ll receive a B+ just for effort. Where exactly you actually confused everybody was first on your specifics. As they say, the devil is in the details… And it could not be much more correct in this article. Having said that, let me tell you exactly what did work. The text is definitely highly convincing and that is most likely the reason why I am making an effort in order to comment. I do not make it a regular habit of doing that. Next, whilst I can notice the jumps in logic you come up with, I am definitely not convinced of how you seem to connect the points which inturn produce your conclusion. For right now I will, no doubt subscribe to your issue however trust in the future you actually link the facts better.

    2012/11/21
  2. I liked up to you will obtain carried out right here. The sketch is tasteful, your authored material stylish. however, you command get bought an impatience over that you would like be handing over the following. sick for sure come further in the past once more as exactly the similar nearly very continuously inside of case you protect this increase.

    2013/03/17
  3. Tip #5: Go LocalMany people think that one can buy include non acidic
    juices, protein powder, skimmed milk, oatmeal,
    for newer may breads the slim green reduce cream review and sassy program.
    4 The next question is whether the application raises
    a serious question affecting the interpretation or application of the convention.

    A rescue worker had also described the chance of survivors being found as” Slim Green Reduce Cream Review” kid in school.

    2013/06/12
  4. Thanks for every other informative site. Where else may just I am getting that
    tpe of info written in such a perfect way? I’ve a challenge that I am simply now running on, and I have been at the
    look out for such information.

    2014/09/23

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: