VBscript to delete files older than X day from folder and subfolders

Hello all,

I have been trying to set up a VB script to delete all files older than 2 days in a specific folder and all subfolders without deleting the folders.  I found a great script presented by an Expert on this site - RobSampson.  When I have tried to run the script I get an error message:

"Object doesn't support this property or message" 800A01B6.

The line being flagged is line 13, character 1 which equates to the first "For Each oSubFolder in oFolder"  Probably a spelling error somewhere on my part but I can't see it.  The only thing I noted is that I don't see oSubFolder defined anywhere previously in the script.

Here is the script:

iDaysOld = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\Temp"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
'If database log file backups are older than 1 days, delete them.
For each oFile in oFileCollection
      If oFile.DateLastModified < (Date() - iDaysOld) Then
            oFile.Delete(True)
      End If
Next

For Each oSubFolder In oFolder 'SCRIPT BOMBS HERE
      RecurseFolders oSubFolder
Next

'Clean up
Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

Sub RecurseFolders(oFolder)
      set oFileCollection = oFolder.Files
      'If database log file backups are older than 1 days, delete them.
      For each oFile in oFileCollection
            If oFile.DateLastModified < (Date() - iDaysOld) Then
                  oFile.Delete(True)
            End If
      Next      
      For Each oSubFolder In oFolder
            RecurseFolders oSubFolder
      Next
End Sub

RobSampson....you out there?
yccdadminsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yccdadminsAuthor Commented:
Forgot to mention - environment is Windows 2008 R2 server.
0
KimputerCommented:
oFolder should be oFolder.Subfolders on the line it "bombs"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yccdadminsAuthor Commented:
Looks like that may have been it - excellent!  I'm getting a permissions error now and that is probably due to specific system files - I'm testing on old user profiles.
0
KimputerCommented:
In that case, try to incorporate:

'at the top
On Error Resume Next

'right after delete
if  Err.Number <> 0  then  
  'code some text  file logging to list the files giving you errors
end
Err.Clear
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.