I am trying to figure out how to recursively delete files 15 days or
older along with any folders that are empty. I can not seem to figure
out how to get the file and folder count needed in the folder it's
currently in. The script is below in its entirety.
Thanks in advance.
~Daniel
'<START>
'*********************************************************************
DeleteTemp "C:\Temp", 15
'*********************************************************************
Sub DeleteTemp(sDirPath, iAge)
'On Error Resume Next
Dim oFolder
Dim oFileCollection
Dim oFile
Dim oFolderCollection
Dim oSubFolder, intErrNum
Dim objWshShell
Set objWshShell = CreateObject( "WScript.Shell" )
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sDirPath)
Set oFileCollection = oFolder.Files
Set oFolderCollection = oFolder.SubFolders
'Walk threw each file in this folder collection
For Each oFile In oFileCollection
'On Error Resume Next
If DateDiff("D", oFile.DateCreated, Date) >= iAge Then
oFile.Delete(True)
End If
Next
For Each oSubFolder In oFolderCollection
'On Error Resume Next
msgbox "Checking folders"
'======================>If Files = 0 AND SubFolders = 0 Then
oSubFolder.Delete(True)
Else
DeleteTemp oSubFolder.Path, iAge
End If
Next
'Clean Up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing
Set oFolderCollection = Nothing
Set oSubFolder = Nothing
Set objWshShell = Nothing
End Sub
'<END>