Age-Based File Deletion in WSH - Adding some error-handling
(Page 2 of 5 )
When I create scripts I like to account for as many different scenarios as possible. This script is no different. It’s hard to say what kind of uses different people may have for something like this. That also means that there’s no telling what files they may try to delete.
In Windows you will receive an error any time you try to delete a file that is read-only. With that in mind we should attempt to work around that.
We could easily add an If statement to check the file’s attributes and change them whenever necessary, but that would be more work that is really required. There’s no point wasting space checking a file’s attributes when we can simply reset them before we delete. Let’s take a look at the code.
Sub DeleteOldFile(objFile, numDays)
dateFile = objFile.DateLastModified
dateToday = Now()
If dateFile <= dateToday Then
daysOld = dateToday - dateFile
If daysOld > numDays Then
objFile.Attributes = 0
objFile.Delete
End If
Else
WScript.Echo "Incorrect date stamp in", strFile
End If
End Sub
Quite simply we’re using the File object’s Attributes property to reset any attributes prior to calling the Delete method. There’s no harm in setting this attribute in every case so there’s no need to create any If statement at all.
There is still one last scenario to consider—an undeletable file. Since we’ve reset the file’s attributes, the only scenario left that would prevent a file from being deleted is if the file was currently in use.
This would raise an error and cause our script to quit. For obvious reasons, we don’t want that to happen. We want our script to continue on to the next file. In order for that to happen, we need to turn off error handling with the On Error Resume Next statement.
Sub DeleteOldFile(objFile, numDays)
On Error Resume Next
Err.Clear
dateFile = objFile.DateLastModified
dateToday = Now()
If dateFile <= dateToday Then
daysOld = dateToday - dateFile
If daysOld > numDays Then
strFile = objFile.Path
objFile.Attributes = 0
objFile.Delete
If Err.number <> 0 Then
WScript.Echo Err.number, Err.Description, Err.Source, _
strFile
Call RemoveOnReboot(strFile)
End If
End If
Else
WScript.Echo "Incorrect date stamp in", strFile
End If
End Sub
I’ve made a few final changes to complete the DeleteOldFile subroutine. These changes are mostly for error-handling purposes.
First, I’ve made sure to clear the Err object after entering the subroutine. Then I’ve done a simple error check to see if the object file was able to be deleted. A 0 return code indicates a successful deletion, anything else indicates an error. For instance, an error code of 70 would indicate that the target file was in use.
If there was an error, I can safely assume it’s because the file is in use. That means that the file should be scheduled for deletion after the next reboot. We’ll get in to that later. For now, just pass the file path to the RemoveOnReboot subroutine that we’ll be creating later.
Next: Allowing for automated deletion in a folder >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer