|
-
Apr 5th, 2000, 10:09 PM
#1
Thread Starter
Member
Deleting empty folders
Can anyone help me with a little problem I am having? I need to search through a folder for any other empty folders within it, and delete the empty folder.
Ex. c:\temp -- look in the temp folder for any other folders that are empty.
If I find an empty folder in the c:\temp dir, then I will delete that empty folder
Thanks for the Help,
Kpoe
-
Apr 6th, 2000, 02:07 AM
#2
Hyperactive Member
Setup:
1 DirListBox
- Name: Dir1
1 FileListBox
- Name: File1
1 Command Button
- Name: Command1
Code:
Sub Command1_Click()
Dim A, TempPath As String, TotalKilled
TempPath = "Your Temporary Directory"
Dir1.Path = TempPath
For A = 0 To Dir1.ListCount - 1
Dir1.Path = Dir1.List(A)
File1.Path = Dir1.Path
If Dir1.Path Like TempPath + "*" And File1.ListCount = 0 Then RmDir Dir1.Path: TotalKilled = TotalKilled + 1
Dir1.Path = TempPath
Next
Msgbox "Removed " & TotalKilled & " directories from " + Chr(34) + TempPath + Chr(34) + ".", vbInformation
End Sub
I think this will work, I haven't tested it though, so save your work. Don't worry though, if it doesn't work, it won't be deleting anything.
-
Apr 6th, 2000, 11:09 PM
#3
Thread Starter
Member
well it worked for most of the files except if the folder has another folder inside of it. If it does it gives an error 5. Help please
Kpoe
-
Apr 7th, 2000, 12:22 AM
#4
Fanatic Member
I think in DOS, there is some command to take care of this.
I know for sure it does a XCOpy on /e empty files. If you can find the command in DOS, why not use VB to write a batch file and use the Shell function?
Chemically Formulated As:
Dr. Nitro
-
Apr 7th, 2000, 01:16 AM
#5
Hyperactive Member
Whoops, sorry, didn't think of that.
Here:
Setup:
1 DirListBox
- Name: Dir1
1 FileListBox
- Name: File1
1 Command Button
- Name: Command1
Code:
Sub Command1_Click()
Dim A, TempPath As String, TotalKilled
TempPath = "Your Temporary Directory"
Dir1.Path = TempPath
On Error GoTo Errs
For A = 0 To Dir1.ListCount - 1
Dir1.Path = Dir1.List(A)
File1.Path = Dir1.Path
If Dir1.Path Like TempPath + "*" And File1.ListCount = 0 Then RmDir Dir1.Path: TotalKilled = TotalKilled + 1
Dir1.Path = TempPath
Next
Msgbox "Removed " & TotalKilled & " directories from " + Chr(34) + TempPath + Chr(34) + ".", vbInformation
Exit Sub
Errs:
If Err.Number = 5 Then
Dir1.Path = Dir1.List(A + 1)
File1.Path = Dir1.Path
Resume
End if
Msgbox "Error " & Err.Number & ":" + vbCrLf + vbCrLf + Err.Description
End Sub
I have no idea if this works or not, I'm not really thinking right now. Email me if it doesn't work, I don't have time to create blank folders and start a new project. Hope it works.
-
Apr 7th, 2000, 01:32 AM
#6
Addicted Member
Alright kpoe here's some code I just wipped up, I tested it a little and it seems to work just fine, anyway here it is:
Code:
Private Sub GetSubFolders(sDir As String, Optional iRecursive As Long)
Dim iMsgReturn As Integer
Dim idx As Long
Dim iUpperLimit As Long
Dim sFolder() As String
If IsMissing(iRecursive) = True Then
iRecursive = 0
End If
ReDim sFolder(idx)
sFolder(idx) = Dir(sDir, vbDirectory Or vbNormal Or vbHidden Or vbReadOnly Or vbSystem)
Do While sFolder(idx) <> vbNullString
idx = idx + 1
ReDim Preserve sFolder(idx)
sFolder(idx) = Dir
Loop
If idx <= 2 Then
iMsgReturn = MsgBox("Erase the following directory?" & vbCrLf & sDir, _
vbYesNo + vbQuestion + vbApplicationModal, "DELETE")
If iMsgReturn = vbYes Then
Call RmDir(sDir) 'Only deletes if directory is empty.
End If
Else
iUpperLimit = UBound(sFolder) - 1
For idx = 0 To iUpperLimit Step 1
If GetAttr(sDir & sFolder(idx)) = vbDirectory _
And sFolder(idx) <> "." _
And sFolder(idx) <> ".." Then
Call GetSubFolders(sDir & sFolder(idx) & "\", iRecursive + 1)
End If
Next idx
End If
Erase sFolder
If iRecursive = 0 Then
Call MsgBox("Finished")
End If
End Sub
To call the function simply pass it the name of the root directory where you wish to start the search in.
For example:
Code:
Call GetSubFolders("C:\Windows\")
Incase you were wondering, the iRecursive parameter is used to keep track of what level of the directory you are at (0 being the root of the directory you passed). It isn't really used or needed in the function, I just included it so you could use it if you needed to, like to tell at what level of the directory you are at when debugging.
Let me know if this is what you were looking for.
Ciao for now.
[Edited by SonGouki on 04-07-2000 at 02:35 PM]
-
Apr 9th, 2000, 10:22 PM
#7
Thread Starter
Member
Thanks for all of the help
the program works great -- SonGonki
-
Apr 9th, 2000, 11:59 PM
#8
Fanatic Member
Hey SonGouki!
That is one bad ass program. I like it! Also, your programming style is cool, it is similar to mine.
See Ya
Chemically Formulated As:
Dr. Nitro
-
Apr 10th, 2000, 12:31 AM
#9
Addicted Member
Thanks for the cudos guys, always happy to help. BTW the code I post is not usually my true coding style, I just format so that is more readable to the beginning programmer. I use a modified version of the Microsoft consultants' coding guidelines for all my projects (I'm not a MS consultant).
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
|