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>

Re: Deleting Empty Folders Recursively by ekkehard

ekkehard
Wed Nov 28 09:28:21 PST 2007

BlueMonkeyFish schrieb:
> 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>

As you must have worked on the files *and* the subfolders
of a given directory *before* you can decide whether to delete
it or not, the "delete folder action" should be the last part
of the recursive subroutine:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sDir : sDir = oFS.GetAbsolutePathName( ".\zaptmp\zap" )

DeleteTemp oFS.GetFolder( sDir ), 15

Sub DeleteTemp( oFld, nDays )
Dim oItem
For Each oItem In oFld.Files
WScript.Echo oItem.DateCreated, oItem.Path
If DateDiff( "d", oItem.DateCreated, Date ) > nDays Then
WScript.Echo "will delete", oItem.Path
oItem.Delete True
End If
Next
For Each oItem In oFld.SubFolders
DeleteTemp oItem, nDays
Next
If 0 = oFld.Files.Count And 0 = oFld.SubFolders.Count Then
WScript.Echo "will delete", oFld.Path
oFld.Delete True
End If
End Sub

I didn't test this as carefull as I should, but I hope this will
get you started.