Results 1 to 6 of 6

Thread: [RESOLVED] recursive search for empty folders

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    7

    Resolved [RESOLVED] recursive search for empty folders

    Hi I was wondering if anyone could help me with the problem im having trying to write a program. i've been trying to figure it out for a long time but no luck. im about to give up on it so i hope someone will be able to help me out. I am trying to write a program that cleans up a partition. Its supposed to run everytime computer is turned on and it'll scan a partition.. lets say D:\ for files/folders and its supposed to delete any files that are older that x days (lets say 7). Also its supposed to check folders/subfolders to see if they
    are empty.. if so, its supposed to delete that empty folder. Now, i got everything to work except one thing.. Lets say my partition (D:\) contains Folder 1 and Folder 2. Inside Folder 1, there are subfolders called Folder 1.1 and Folder 1.2. and inside subfolder "Folder 1.1" lets say there's another subfolder called "Folder 1.1.1" (which is empty) .So when the program scans for empty folder (using recursion), it'll start with D:\ then go inside Folder 1 which contains Folder 1.1 and Folder 1.1 Contains Folder 1.1.1 which is empty.. So it deletes it.. that part is fine but the problem is.. after Folder 1.1.1 gets deleted, it just goes straight to Folder 2.. I dont want that cause after Folder 1.1.1 is deleted, Folder 1.1 is now empty since Folder 1.1.1 doesn't exist anymore.. I want the program to go back to Folder 1.1 and see if its empty.. if it is, then delete it.. but it doesn't do that.. Im having hard time figuring out the concept to get that to work.. basically what i would like the program to do is pick a folder (for eg Folder 1) in the root drive and go to the deepest subfolder of that folder and check if that's empty, if it is, then delete that and go back up one level to its parent folder and check if that's empty... and so on... and finally go back to root. Then it'll
    continue doing the same thing with other folders in the root drive... i hope im making sense. if you have any questions pls lemme know.. and any help/suggestions would be really appriciated. thanks

    Im using the procedure below to search for empty folders:
    VB Code:
    1. Public Sub SearchEmptyFolder(srcFol As String)
    2.  
    3. Dim fso As New FileSystemObject
    4. Dim fld As Folder, sFld As Folder
    5.  
    6. Set fld = fso.GetFolder(srcFol)
    7.  
    8.   If fld.SubFolders.Count > 0 Then
    9.       For Each sFld In fld.SubFolders
    10.          If sFld.Files.Count + sFld.SubFolders.Count > 0 Then
    11.              SearchEmptyFolder sFld.Path
    12.          Else
    13.              'Its an empty folder so delete it
    14.               MsgBox "The folder " & sFld.Path & " is empty so it has been deleted."
    15.               'fso.DeleteFolder (tFld.Path)
    16.          End If
    17.  
    18.        Next
    19.    End If
    20.  
    21. End Sub



    Edit: Added [vbcode][/vbcode] tags for clarity. - Hack
    Last edited by Hack; Sep 22nd, 2005 at 08:34 AM.

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    7

    Re: recursive search for empty folders

    hey rhinobull thanks for the friendly welcom and also quick reply... i had already visited the site you gave me and i had tried using some of the code there without any luck... i think what happens with recursion is, once it gets to the deepest subfolder, it doesn't go back up one level cause since that folder has already been looked at.. but i might be wrong... thanks for the link though.. i really appriciate it.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    7

    RESOLVED recursive search for empty folders

    my problem's solved. so just updating the status... thanks rhino for help..

  5. #5

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2005
    Posts
    7

    Re: [RESOLVED] recursive search for empty folders

    sorry haven't been able to use the computer for more than a week... but all i needed to do was type the following code outside the loop


    If fld.SubFolders.Count + fld.Files.Count = 0 Then
    'Its an empty folder (now) so delete it
    MsgBox "The folder " & sFld.Path & " is empty so it has been deleted."
    'fso.DeleteFolder (tFld.Path)
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width