Recursively delete outdated files with VBScript

Recently, I came across the situation, where I had to delete outdated SQL backup files from our MS SQL servers Backup directory. To tidy up the backup folder, I wrote a small VB script which handles this for me:

' DeleteOutdated
' Parameters
' MaxFileAge: Maximum file age in days (modification date)
' Path: Folder which contains the files
' VBScript ref.
' FSO ref.

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

Sub DeleteOutdated(Path, MaxFileAge)
	Set objDir = objFso.GetFolder(Path)
	For Each objFile in objDir.Files
		If objFile.DateLastModified < (Date() - MaxFileAge) Then
			AskDelete = MsgBox("Delete "&objFile.Path&"?",3,"Delete file")
			If AskDelete = 6 Then
			End If
			If AskDelete = 2 Then
			End If
		End If

	For Each objSubfolder in objDir.Subfolders
		Call DeleteOutdated(objSubfolder, MaxFileAge)
End Sub

Call DeleteOutdated("c:\outdatedstuff", 1)

In this state, the script asks to delete everytime, if it finds a file wich is older than specified. Of course, you might want to remove that in productive usage, when script execution is scheduled.

Here I attached a version, which writes a logfile and processes empty subfolders (updated on 26.08.2009):