Results 1 to 6 of 6

Thread: Including subdirectories?

  1. #1

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

    Question Including subdirectories?

    I have this code:
    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
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Your going to have to use recursion to implement what you want.

    Basically, you are going to want to create a seperate Function that calls itself for each subdirectory until there is no subdirectories left. Then that function will start returning the files to the calling function (which will be itself most of the time).

  3. #3

    Thread Starter
    Addicted Member ProgrammerJon's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    203
    So there is no easy way to do it like includeSubdirectories = True?

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Not that I know of...maybe someone else knows. The code isn't that hard to do. When I get home I will try to remember to whip something up for you if you need it.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    This gets all the sub folders, you can easily modify it to work for files if you want. At least this can help get you started.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
    2. System.EventArgs) Handles MyBase.Load
    3.         ListBox1.Items.Clear()
    4.         ProcessFolder("c:\inetpub\")
    5.     End Sub
    6.  
    7.     Private Sub ProcessFolder(ByVal Path As String)
    8.         Dim objDI As New System.IO.DirectoryInfo(Path)
    9.         Dim objFolders As System.IO.DirectoryInfo()
    10.         Dim objFolder As System.IO.DirectoryInfo
    11.  
    12.         objFolders = objDI.GetDirectories
    13.         For Each objFolder In objFolders
    14.             Debug.WriteLine(objFolder.FullName)
    15.             ListBox1.Items.Add(objFolder.FullName)
    16.             ProcessFolder(objFolder.FullName)
    17.         Next
    18.  
    19.     End Sub

  6. #6

    Thread Starter
    Addicted Member ProgrammerJon's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    203
    I did actuly get my program to search in the subdirectories, but thanks for the code.

    I'm suprised that you can't do this very easly, when I was programming in vba 6.0 it was really easy;
    all you had to do was something like this:
    application.filesearch.includesubdirectories = true

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