|
-
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.
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
|