|
-
Sep 22nd, 2005, 08:23 AM
#1
Thread Starter
New Member
[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:
Public Sub SearchEmptyFolder(srcFol As String)
Dim fso As New FileSystemObject
Dim fld As Folder, sFld As Folder
Set fld = fso.GetFolder(srcFol)
If fld.SubFolders.Count > 0 Then
For Each sFld In fld.SubFolders
If sFld.Files.Count + sFld.SubFolders.Count > 0 Then
SearchEmptyFolder sFld.Path
Else
'Its an empty folder so delete it
MsgBox "The folder " & sFld.Path & " is empty so it has been deleted."
'fso.DeleteFolder (tFld.Path)
End If
Next
End If
End Sub
Edit: Added [vbcode][/vbcode] tags for clarity. - Hack
Last edited by Hack; Sep 22nd, 2005 at 08:34 AM.
-
Sep 22nd, 2005, 08:43 AM
#2
Re: recursive search for empty folders
Welcome to Forums, boka!
I would recommend to check out this sample written by Randy Birch. It demonstrates how to "properly" do recursive search. There are many other samples available.
-
Sep 22nd, 2005, 08:49 AM
#3
Thread Starter
New Member
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.
-
Sep 26th, 2005, 10:36 AM
#4
Thread Starter
New Member
RESOLVED recursive search for empty folders
my problem's solved. so just updating the status... thanks rhino for help..
-
Sep 26th, 2005, 12:04 PM
#5
Re: [RESOLVED] recursive search for empty folders
That sounds great but also you may want to share your solution so other can benefit. Thanks.
-
Oct 6th, 2005, 02:07 PM
#6
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|