2 Replies - 1718 Views - Last Post: 30 March 2011 - 10:03 AM Rate Topic: -----

#1 astroshark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Recursively Search Folder and place that list into a ListBox

Posted 30 March 2011 - 01:32 AM

I am trying to make a program where I browse for a folder and that Browse dialog displays user selection into a Text Box that part I have completed, the part I need help with is when that user selects the folder it will recursively search all folders and subfolders in the directory the user has chosen and display that list in a ListBox here is the code I have so far. Also to note I have found a function for recursively search a folder but I have no idea how to implement it to display in the ListBox from the User selected directory.

This code is the code that lets the user select the folder and display it in the textbox
    Private Sub SourceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceButton.Click
        With SourceBrowse
            If .ShowDialog = DialogResult.OK Then
                SourcePath.Text = .SelectedPath
            End If
        End With
    End Sub



This code is the function I found and tweaked to recursively search a folder
    Private Function RecursiveSearch(ByVal path As String) As Boolean
        Dim dirInfo As New IO.DirectoryInfo(path)
        Dim fileObject As FileSystemInfo
        For Each fileObject In dirInfo.GetFileSystemInfos()
            If (fileObject.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
                RecursiveSearch(fileObject.FullName)
            Else
                Console.WriteLine(fileObject.FullName)
            End If
        Next
        Return True
    End Function



On a side note I also want the list to display all the files in the folder as well.

This post has been edited by astroshark: 30 March 2011 - 01:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Recursively Search Folder and place that list into a ListBox

#2 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: Recursively Search Folder and place that list into a ListBox

Posted 30 March 2011 - 06:49 AM

You will need to ensure that the RecursiveSearch code has been copied into the same form as the below code.

Alter the top section of code to the following:

Private Sub SourceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceButton.Click
    With SourceBrowse
        If .ShowDialog = DialogResult.OK Then
            SourcePath.Text = .SelectedPath
            RecursiveSearch(.SelectedPath) // Start the search
        End If
    End With
End Sub



You will then need to modify RecursiveSearch to Add to a list box instead of writing to the console:
Assuming your listbox is calld lstDirectory


Private Function RecursiveSearch(ByVal path As String) As Boolean
    Dim dirInfo As New IO.DirectoryInfo(path)
    Dim fileObject As FileSystemInfo
    For Each fileObject In dirInfo.GetFileSystemInfos()
        If (fileObject.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
            RecursiveSearch(fileObject.FullName)
        Else
            Me.lstDirectory.Add(fileObject.FullName) // Add to listbox instead of outputting to console
        End If
    Next
    Return True
End Function



Hope this helps
Was This Post Helpful? 0
  • +
  • -

#3 astroshark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 25-November 09

Re: Recursively Search Folder and place that list into a ListBox

Posted 30 March 2011 - 10:03 AM

That helped a lot thank you so much but also on a side not for the recursive function tag that you changed I had to add Items before Add to make it work like so.

Before
ItemList.Add(fileObject.FullName) ' Add to listbox instead of outputting to console



After
ItemList.Items.Add(fileObject.FullName) ' Add to listbox instead of outputting to console


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1