Hi,
I'm working on a script to delete all files and folders that are older than a specific number of days (for example, 60 days). So the script will recurse through all folders for the given input folder, check how old it is, and then delete it. Also, if a folder is empty, it deletes it.
The problem is, whenever it is encountering the very first empty folder, it is deleting it, and then immediately exiting out of the script without printing anything. So basically at the first sign of empty folder the script is simply dying.
Here are the bits for iterating through the folders and deleting them.
' This line is in the main program which calls the function ShowSubFolders
ShowSubFolders objFSO3.GetFolder(pathtofolder1)
' This is the function ShowSubFolders
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO3.GetFolder(Subfolder.Path)
' Enumerating folders
objLogfile.Write(Subfolder.Name & ",")
If Subfolder.Size = 0 Then
temppath = Subfolder.Path
objFSO3.DeleteFolder Subfolder.Path ' DELETES the empty folder
If Err.Number = 0 Then
objLogfile.Write("Folder - EMPTY," & temppath & "," _
& Subfolder.DateLastAccessed & ",-,Deleted" _
& vbCrLf)
Else
objLogfile.Write("Folder - EMPTY," & temppath & "," _
& Subfolder.DateLastAccessed & ",-,Error Deleting" _
& vbCrLf)
End If
Else
objLogfile.Write("Folder - NOT Empty," & objFolder.Path _
& "," & Subfolder.DateLastAccessed & ",-,-," _
& vbCrLf)
End If
' Enumerating files
Set colFiles = objFolder.Files
For Each objFile in colFiles
intDaysDifference = DateDiff("d", objFile.DateLastAccessed, Date)
objLogfile.Write (objFile.Name & "," & "File" & "," _
& Subfolder.Path & "," & objFile.DateLastAccessed & "," _
& intDaysDifference & " days" & ",")
If Abs(intDaysDifference) > Abs(numberofdays) Then
objFSO3.DeleteFile Subfolder.Path & "\" & objFile.Name
objLogfile.Write ("Deleted" & vbCrLf)
Else
objLogfile.Write ("No" & vbCrLf)
End If
Next
ShowSubFolders Subfolder
Next
End Sub
It's deleting the folder as intended, but not sure why it is not printing the output and simply exiting.
Any pointers?
Cheers
<message edited by sun1010 on Wednesday, February 01, 2012 10:47 PM>