• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Delete Files (Only) Recursively Script

Hello, Can someone plaese take a look at my script below. I need to delete all files recursively according to file age but should not include sub-folders. Please help. Thank you!

'This script delete all files recursively in a specific directory location using specific file age.

' Folder to delete files
strFolder = "C:\Folder\"
' Files older than this will be deleted.
intDays = 1
 
Set objFSO      = CreateObject("Scripting.FileSystemObject")
Set objFolders      = objFSO.GetFolder(strFolder)
objToday      = Now()
objPastDate      = DateAdd("d", intDays*-1, objToday)
 
Recurse objFolders
 
Sub recurse(ByRef objFolders)
    Set objSubFolders = objFolders.SubFolders
    Set objFiles = objFolders.Files
 
    For Each File In objFiles
        If File.DateLastModified < objPastDate Then
            On Error Resume Next
            File.Delete
        End If
    Next
   
    For Each Folder In objSubFolders
        If Folder.DateLastModified < objPastDate Then
            On Error Resume Next
            objFSO.DeleteFolder Folder.Path, True
        Else
            recurse Folder
        End If
    Next
 
    Set objSubFolders = Nothing
    Set objFiles = Nothing
   
End Sub
0
mmoya
Asked:
mmoya
  • 2
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Huh?...your description is contradictory:

    "I need to delete all files recursively according to file age..."

Recursively here, means go into subfolders (no matter how deep), and also delete files according to age.

Finishing the sentence though:

    "...but should not include sub-folders."

So you do, or do not, want to recurse?

If you do not want to recurse, then simply remove this portion:

    For Each Folder In objSubFolders
        If Folder.DateLastModified < objPastDate Then
            On Error Resume Next
            objFSO.DeleteFolder Folder.Path, True
        Else
            recurse Folder
        End If
    Next
0
 
mmoyaAuthor Commented:
Sorry about that. I want to delete files (regardless how deep they get) but not delete their sub-folders.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Gotcha...change:

    For Each Folder In objSubFolders
        If Folder.DateLastModified < objPastDate Then
            On Error Resume Next
            objFSO.DeleteFolder Folder.Path, True
        Else
            recurse Folder
        End If
    Next

To:

    For Each Folder In objSubFolders
        recurse Folder
    Next
0
 
mmoyaAuthor Commented:
I'll try it now..thank you! I'll let you know. Thanks again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now