• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • 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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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