Script to delete files after x days

Have you ever needed to automate the cleaning of a folder that say is used as part of a backup or just as a dumping ground.  Using this script will help you to speed up the cleaning process by deleting files from any folder or UNC path that are older than the specified (x) days.

This is ideal to use if files are only added to the folder.  If files are subsequently modified, these files may be lost.

The script works by browsing through the directory identifying any files that are older than the specified number of days.  If files are found, they are added to an array (effectively tagged for deletion) and then deleted from the disk.  The deletion method will erase the file meaning that the recycle bin will never see the deleted files.The

The script is a modified version of a script that I found on the Internet.  I can’t remember who the author was or which site it came from, so I won’t take credit for any it.

Use at your own Risk!

Copy the code below and paste into WordPad.  Save the file as delold.vbs

‘ delold.vbs
‘ ***************************************************
‘ * This script will delete all files in a folder and
‘ * sub-folder where when files are x number of days
‘ * old.
‘ *
‘ * Files will be deleted and not retained in the
‘ * recycle bin.
‘ *
‘ * Alter the path and lifetime variables to your needs

‘ * Alter this variable, this is the starting point
‘ * for the deletion.
‘ * Example : path = “teacherbackup”
‘ * Example : path = “N:\Logfiles”


‘ * Alter this variable, if sub-folders should be processed
‘ * Process Sub folders Example : Subfolders = True
‘ * Process Sub folders Example : Subfolders = False

Subfolders = False
‘ * Alter this variable to set the how many days old
‘ * the file should be before it is deleted.

‘ * Example : lifetime = date() – 20
‘ * Will delete files that are 20 days old or more

Lifetime = date() – 20

‘ *******************************************************
‘ Script starts at this point
‘ *******************************************************
‘ Declare a variable as an Array that will store a listing
‘ of files that will be checked.

FilesArray = Array()

‘ Create an instance of the FileSystemObject so that file
‘ information can be obtained.

set fso = createobject(“scripting.filesystemobject”)

‘ Call the SelectFiles procedure to Fill the array with
‘ files and folders that will be deleted

SelectFiles path, lifetime, FilesArray, Subfolders

‘ Process the FilesArray deleting files as we loop through

numDeleted = 0

for n = 0 to ubound(FilesArray)
‘ Switch off error checking, so that errors are ignored
on error resume next
‘ Call the delete function to delete the selected file
FilesArray(n).delete true

‘ Handle any errors or results. This could be modified
‘ to log to a text file.
‘if err.number <> 0 then
‘ wscript.echo “Unable to delete: ” & FilesArray(n).path
‘ numDeleted = numDeleted + 1
‘end if
‘ Switch Error checking back to normal
on error goto 0

sub SelectFiles(sPath,vlifetime,FilesArrayToKill,bIncludeSubFolders)
‘ Switch off Error handling, errors ignored.
on error resume next
‘select files to delete and add to array…
set folder = fso.getfolder(sPath)
set files = folder.files

‘ Loop through files that have been found
for each file in files
‘ uses error trapping around access to the
‘ Date property just to be safe

dtlastmodified = null
on error resume Next
dtlastmodified = file.datelastmodified
on error goto 0

if not isnull(dtlastmodified) Then
if dtlastmodified < vlifetime then
count = ubound(FilesArrayToKill) + 1
redim preserve FilesArrayToKill(count)
set FilesArrayToKill(count) = file
end if
end if

‘ If sub-folders are selected, call the procedure again to update
‘ the array with the contents.

if bIncludeSubFolders then
for each fldr in folder.subfolders
SelectFiles fldr.path,vlifetime,FilesArrayToKill,true
end if
end sub



Posted in Scripting and tagged , , , .