Results 1 to 2 of 2

Thread: Including subdirectories?

  1. #1

    Thread Starter
    Addicted Member ProgrammerJon's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    203

    Including subdirectories?

    I have this code that searches a directory:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' make a reference to a directory
            Dim di As New IO.DirectoryInfo("c:\")
            Dim diar1 As IO.FileInfo() = di.GetFiles()
            Dim dra As IO.FileInfo
    
            'list the names of all files in the specified directory
            For Each dra In diar1
                ListBox1.Items.Add(dra)
            Next
        End Sub
    How do I make it so that it includs subdirectories?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Create a function to search through subdirectories and then call that recursivly to search each subdirectory's subdirectories. Something like this.....

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim rf As New System.IO.DirectoryInfo("d:\VBNET")
        Dim file As System.IO.FileInfo
        For Each file In rf.GetFiles
            ListBox1.Items.Add(file.FullName)
        Next
        getsubdirs(rf)
    
    End Sub
    
    Sub getsubdirs(ByVal rf As System.IO.DirectoryInfo)
    
        Dim sfolders() As System.IO.DirectoryInfo = rf.GetDirectories
        Dim folder As System.IO.DirectoryInfo
        For Each folder In sfolders
            getsubdirs(folder)
            Dim file As System.IO.FileInfo
            For Each file In folder.GetFiles
                ListBox1.Items.Add(file.FullName)
            Next
        Next
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width