Tag Archives: Windows

Deleting Files Recursively by Age Using VBScript

The other day I started scouring the web for a VBScript to use for deleting files that had not been modified for a certain number of days.  I tried several, some of which were designed to delete files in a given folder, some that deleted certain file extensions recursively, but none that did it recursively by age alone.

When I say “age” I mean the DateLateModified property of the file object in question.  In this script, you specify the number of days (most accurate method), and the target folder, and the name of a logfile.txt that you want generated, as well.  With the log file you can look for OK on success and Failed if the delete didn’t work.

I put my scripts in a folder at C:scripts.  I called this one delete.vbs. 

To run this script, copy the code to your favorite text editor, save the file as a .vbs, open a command line to the directory and type “cscript delete.vbs”

Remember to edit:
1. Line 1 which indicates the target folder where files are going to be deleted
2. Line 2 which indicates the logfile that will be created
3. Line 3 which indicates how many days old you’re starting with… files with a DateLastModified OLDER than this date will be deleted permanently.

You should be able to access the logfile after the script runs.

Here’s the code:

VBScript for Recursive Delete
  1. strFolder = "C:\Test"
  2. logfilename = "C:\scripts\testlog.txt"
  3. intDays = 1277
  4. ForAppending = 8
  5. Set objFSO    = CreateObject("Scripting.FileSystemObject")
  6. Set objFolders    = objFSO.GetFolder(strFolder)
  7. objToday        = Now()
  8. objPastDate    = DateAdd("d", intDays*-1, objToday)
  9. Set objOutFile = objFSO.OpenTextFile(logfilename,ForAppending, True)
  10. objOutFile.WriteLine "——————————"
  11. objOutFile.WriteLine "Run at " & Date()
  12. objOutFile.WriteLine "——————————"
  13.  
  14. Recurse objFolders
  15. Sub recurse(ByRef objFolders)
  16.     Set objSubFolders = objFolders.SubFolders
  17.     Set objFiles = objFolders.Files
  18.     For Each File In objFiles
  19.         If File.DateLastModified < objPastDate Then
  20.             On Error Resume Next
  21.             objOutFile.Write "Deleting " & File.Path & "\" & File.Name
  22.             File.Delete
  23.             If Err.Number = 0 then
  24.                 objOutFile.WriteLine "..OK"
  25.             else
  26.                 objOutFile.WriteLine "..Failed"
  27.             end if
  28.             On Error Goto 0
  29.         End If
  30.     Next
  31.     
  32.     For Each Folder In objSubFolders
  33.         recurse Folder
  34.         If Folder.DateLastModified < objPastDate Then
  35.             On Error Resume Next
  36.             objOutFile.Write "Deleting " & Folder.Path & "\" & Folder.Name
  37.             objFSO.DeleteFolder Folder.Path, True
  38.             If Err.Number = 0 then
  39.                 objOutFile.WriteLine "..OK"
  40.             else
  41.                 objOutFile.WriteLine "..Failed"
  42.             end if
  43.             On Error Goto 0
  44.         Else
  45.         End If
  46.     Next
  47.     Set objSubFolders = Nothing
  48.     Set objFiles = Nothing
  49.     
  50. End Sub