Recursive Listing of Files and Directories in vb.net

Below is a technique that I use when I want to do a recursive listing of all files and directories at GE Healthcare.  It has a built-in "do not call" list (those directories that you don’t want to descend into), and it has fairly robust error checking.
 
Imports System.IO
 
Class Traverse2
    Public First As Boolean
    Public f_count, d_count As Integer
 
 
    Sub doit(ByVal path As String)
        Dim p_di, di, di_array() As DirectoryInfo
        Dim fi, fi_array() As FileInfo
        Dim LastDir As String
        Dim i, p_len As Integer
 
        ' skip the recycle bin and other directories we don't want...
        If path.Substring(1).Equals(":\RECYCLER") Or path.Substring(1).Equals(":\System Volume Information") Then
            Exit Sub
        End If
 
        ' dress it up a wee bit...
        If Not path.EndsWith("\") Then
            path = path & "\"
        End If
 
        Try
            p_di = New DirectoryInfo(path)
        Catch
            ' silently ignore these errors
            Exit Sub
        End Try
 
        Try
            ' get the information about the directory itself
            If First Then
                ' *****************
                '  do something here!
                ' *****************
                Debug.WriteLine(p_di.Name)
                First = False
                d_count += 1
            End If
 
            Try
                fi_array = p_di.GetFiles
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
                Exit Sub
            End Try
 
            For Each fi In fi_array
                ' *****************
                '  do something here!
                ' *****************
                Debug.WriteLine(fi.Name)
                f_count += 1
            Next
 
            ' Process the directories (and descend into dir structure)
            Try
                di_array = p_di.GetDirectories
            Catch ex As Exception
                ' This probably redundant, since we catch this type of
                ' error above while processing files.
                Debug.WriteLine(ex.Message)
                Exit Sub
            End Try
 
            di_array = p_di.GetDirectories
            For Each di In di_array
                ' *****************
                '  do something here!
                ' *****************
                Debug.WriteLine(di.Name)
                d_count += 1
 
                ' recursive call to this subroutine
                doit(di.FullName)
            Next
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
 
    End Sub
End Class

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: