Age-Based File Deletion in WSH - Allowing for automated deletion in a folder
(Page 3 of 5 )
Most of the requests I’ve gotten were from people looking for a script that could be used as a Scheduled Task for automatically removing old backups from an archive folder. With that purpose in mind, I decided to take this script a step further and add a subroutine that would allow the script to move through all of the files in a specified folder.
Sub CheckFolder(objFolder, numDays)
Set colFiles = objFolder.Files
If colFiles.Count > 0 Then
For Each objFile In colFiles
Call DeleteOldFile(objFile, numDays)
Next
Else
WScript.Echo "No files in folder", objFolder.Path
End If
End Sub
The subroutine is pretty simple. I use the Files property to return a collection of files in the specified folder location. A simple check ensures that there were indeed files found and then call the DeleteOldFile method for each of them.
To add a little flexibility, I’ve added numDays as a parameter for this subroutine. That allows you to specify a different age for each folder.
Let’s suppose for a minute that someone wanted to clean an entire folder tree. That means they would want to delete files recursively, or in all subfolders, as well. We can add recursion quite easily.
Sub CheckFolder(objFolder, numDays, bRecurse)
Set colFiles = objFolder.Files
If colFiles.Count > 0 Then
For Each objFile In colFiles
Call DeleteOldFile(objFile, numDays)
Next
Else
WScript.Echo "No files in folder", objFolder.Path
End If
If bRecurse Then
Set colSubfolders = objFolder.SubFolders
If colSubfolders.Count > 0 Then
For Each SubFolder In colSubfolders
CheckFolder SubFolder, numDays, True
Next
End If
End If
End Sub
Again keeping flexibility in mind, I’ve added bRecurse as a parameter to the subroutine. bRecurse is a Boolean value the indicates whether or not the folder should be processed with recursion.
If the bRecurse value is set to true, the SubFolders property is called to return a colSubfolders collection. A quick check makes sure that the count is not equal to 0 before passing each subfolder back to the CheckFolder subroutine.
Now we can complete the script by making a call to the CheckFolder subroutine. Just add a line to the beginning of your script.
numDays = 7
strPath = "C:\Windows\Temp"
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.FolderExists(strPath) Then
Set objFolder = objfso.GetFolder(strPath)
Call CheckFolder(objFolder, numDays, True)
Else
WScript.Echo "The specified folder", strPath, "does not exist"
End If
Of course, you probably know me by now. I would never stop there. I’ve implemented the FileSystemObject’s FolderExists method to verify the folder path before trying to connect to the Folder object.
In the above example, I’ve set the script to recursively delete all files in the Windows Temp folder that have not been modified in the last seven days.
Now that we’ve got a working script, let’s backtrack slightly and fix one unresolved issue. We still need to create a subroutine that will schedule undeletable files for removal at the next reboot.
Next: Delete files at reboot >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer