View Single Post
  #3 (permalink)  
Old June 11th, 2003, 12:59 PM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Dennis,

I ran into this issue some time ago when I tried to write an application that recursively deleted empty folders within a given folder.

The code I'll post is just an example. It won't win a beauty contest, but it works. Hope you can use it somewhere. If you have any questions, just let me know (through this list).

Code:
Private Sub ProcessDirectory(ByVal targetDirectory As String)
    If Directory.Exists(targetDirectory) And Len(targetDirectory) >= Len(txtDirToProcess.Text) Then
        If Directory.GetFileSystemEntries(targetDirectory).Length = 0 Then
            Dim directoryInfo As System.IO.DirectoryInfo
            directoryInfo = System.IO.Directory.GetParent(targetDirectory)
            Dim theCurrentDir As DirectoryInfo = New DirectoryInfo(targetDirectory)
            ' Check attributes and reset if necessary
            If (theCurrentDir.Attributes And FileAttributes.ReadOnly) > 0 Then
                theCurrentDir.Attributes = theCurrentDir.Attributes And Not FileAttributes.ReadOnly
            End If
            Directory.Delete(targetDirectory)
            ProcessDirectory(directoryInfo.FullName)
        Else
            ' Recurse into subdirectories of this directory
            Dim subdirectory As String
            Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
            For Each subdirectory In subdirectoryEntries
                ProcessDirectory(subdirectory)
            Next subdirectory
        End If
    End If
End Sub

Regards,

Imar
Reply With Quote