Filter Sub-directories

M4cJunk13

New member
Joined
Oct 8, 2014
Messages
2
Programming Experience
Beginner
I posted this on another Forum but haven't been lucky to get any replies and I just found this forum (new to VB.Net) I came across these two code snippets and would like to find a way to combine them. Essentially what I am looking to do is populate a list box with a list of everything in a directory (sub-directories, files) and then use a Textbox to filter out the list.
The top code snippet does a good job at filtering but doesn't dig deep enough down through sub-directories as the second code snippet. Please show me how to combine the two.
Thank you in advance.


VB.NET:
[COLOR=#00008B][FONT=Consolas]Public[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Class[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] Form1[/FONT][/COLOR]  [COLOR=#00008B]Dim[/COLOR] bs [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] BindingSource

  [COLOR=#00008B]Public[/COLOR] [COLOR=#00008B]Sub[/COLOR] [COLOR=#00008B]New[/COLOR]()
    InitializeComponent()
  [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]

  [COLOR=#00008B]Protected[/COLOR] [COLOR=#00008B]Overrides[/COLOR] [COLOR=#00008B]Sub[/COLOR] OnLoad([COLOR=#00008B]ByVal[/COLOR] e [COLOR=#00008B]As[/COLOR] System.EventArgs)
    [COLOR=#00008B]MyBase[/COLOR].OnLoad(e)

    [COLOR=#00008B]Dim[/COLOR] testPath [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String[/COLOR] = [COLOR=#800000]"c:\MyPath"[/COLOR]
    [COLOR=#00008B]Dim[/COLOR] dt [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]New[/COLOR] DataTable
    dt.Columns.Add([COLOR=#800000]"File"[/COLOR], [COLOR=#00008B]GetType[/COLOR]([COLOR=#00008B]String[/COLOR]))

    [COLOR=#00008B]For[/COLOR] [COLOR=#00008B]Each[/COLOR] f [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]String[/COLOR] [COLOR=#00008B]In[/COLOR] Directory.GetFiles(testPath)
      [COLOR=#00008B]Dim[/COLOR] row [COLOR=#00008B]As[/COLOR] DataRow = dt.NewRow
      row([COLOR=#800000]"File"[/COLOR]) = Path.GetFileName(f)
      dt.Rows.Add(row)
    [COLOR=#00008B]Next[/COLOR]
    bs.DataSource = dt

    ListBox1.DisplayMember = [COLOR=#800000]"File"[/COLOR]
    ListBox1.ValueMember = [COLOR=#800000]"File"[/COLOR]
    ListBox1.DataSource = bs
  [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]

  [COLOR=#00008B]Private[/COLOR] [COLOR=#00008B]Sub[/COLOR] TextBox1_TextChanged([COLOR=#00008B]ByVal[/COLOR] sender [COLOR=#00008B]As[/COLOR] [COLOR=#00008B]Object[/COLOR], [COLOR=#00008B]ByVal[/COLOR] e [COLOR=#00008B]As[/COLOR] EventArgs) [COLOR=#00008B]Handles[/COLOR] TextBox1.TextChanged
    bs.Filter = [COLOR=#00008B]String[/COLOR].Format([COLOR=#800000]"File LIKE '*{0}*'"[/COLOR], TextBox1.Text)
  [COLOR=#00008B]End[/COLOR] [COLOR=#00008B]Sub[/COLOR]
 [COLOR=#00008B][FONT=Consolas]End[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] [/FONT][/COLOR][COLOR=#00008B][FONT=Consolas]Class[/FONT][/COLOR]



VB.NET:
Dim stack As New System.Collections.Generic.Stack(Of System.IO.DirectoryInfo)

        'add the start folder
        stack.Push(New System.IO.DirectoryInfo("C:\folder1\"))


        While stack.Count > 0
            Dim di As System.IO.DirectoryInfo = stack.Pop


            'process files in current directory as you need.
            For Each f As System.IO.FileInfo In di.GetFiles


                'here, we just add the filename to a listbox
                Me.ListBox1.Items.Add(f.FullName)


            Next


            Try
                'add subdirectories to the stack to be processed
                For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
                    stack.Push(subDi)
                Next


            Catch ex As System.UnauthorizedAccessException
                'beware access denied errors
                'handle as appropriate
            End Try
        End While
 
In the first snippet, this part:
For Each f As String In Directory.GetFiles(testPath)
    Dim row As DataRow = dt.NewRow
    row("File") = Path.GetFileName(f)
    dt.Rows.Add(row)
Next
puts each file into a DataTable. In the second snippet, this part:
For Each f As System.IO.FileInfo In di.GetFiles
    'here, we just add the filename to a listbox
    Me.ListBox1.Items.Add(f.FullName)
Next
puts each file into a ListBox. Basically, you replace that part in the first snippet with the whole of the second snippet but, in the second snippet, you add the files to the DataTable instead of the ListBox.
 
jmcilhinney,

Thank you for your reply. I'm sorry but I'm not entirely clear as to what exactly I'm replacing. Could you possibly break down a little bit more for me.
 
Basically, you replace that part in the first snippet with the whole of the second snippet

So if you were to replace the part of the code in the first snippet that adds the files to the datatable with everything in the 2nd snippet, you'd get:
PublicClass Form1 Dim bs As New BindingSource

  Public Sub New()
    InitializeComponent()
  End Sub

  Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)

    Dim testPath As String = "c:\MyPath"
    Dim dt As New DataTable
    dt.Columns.Add("File", GetType(String))
    
    ' *** Beginning of 2nd snippet ***

    Dim stack As New System.Collections.Generic.Stack(Of System.IO.DirectoryInfo)
    'add the start folder
    stack.Push(New System.IO.DirectoryInfo("C:\folder1\"))

    While stack.Count > 0
            Dim di As System.IO.DirectoryInfo = stack.Pop

            'process files in current directory as you need.
            For Each f As System.IO.FileInfo In di.GetFiles
                'here, we just add the filename to a listbox
                Me.ListBox1.Items.Add(f.FullName)
            Next

            Try
                'add subdirectories to the stack to be processed
                For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
                    stack.Push(subDi)
                Next
            Catch ex As System.UnauthorizedAccessException
                'beware access denied errors
                'handle as appropriate
            End Try
        End While

    ' *** End of 2nd snippet ***

    bs.DataSource = dt

    ListBox1.DisplayMember = "File"
    ListBox1.ValueMember = "File"
    ListBox1.DataSource = bs
  End Sub

  Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
    bs.Filter = String.Format("File LIKE '*{0}*'", TextBox1.Text)
  End Sub
 EndClass

but, in the second snippet, you add the files to the DataTable instead of the ListBox.

So because of the different in the data-collection objects, you have to go back into the portion with the 2nd snippet and change the code so that it adds the files to the DataTable first instead of directly into the ListBox:
    ' *** Beginning of 2nd snippet ***

    Dim stack As New System.Collections.Generic.Stack(Of System.IO.DirectoryInfo)
    'add the start folder
    stack.Push(New System.IO.DirectoryInfo("C:\folder1\"))

    While stack.Count > 0
            Dim di As System.IO.DirectoryInfo = stack.Pop

            'process files in current directory as you need.
            For Each f As System.IO.FileInfo In di.GetFiles
                'here, we just add the filename to the DataTable
                ' Replace this line: 
                ' Me.ListBox1.Items.Add(f.FullName)
                ' With code for the DataTable:
               Dim row As DataRow = dt.NewRow
               row("File") = f.FullName ' the variable f also changes from a string to a FileInfo object, so use "f.FullName" instead of "Path.GetFileName(f)"
               dt.Rows.Add(row)
            Next

            Try
                'add subdirectories to the stack to be processed
                For Each subDi As System.IO.DirectoryInfo In di.GetDirectories
                    stack.Push(subDi)
                Next
            Catch ex As System.UnauthorizedAccessException
                'beware access denied errors
                'handle as appropriate
            End Try
        End While

    ' *** End of 2nd snippet ***


One last thing to change.... The first snippet uses the variable testPath as the directory to start from, but the 2nd snippet hard-codes in "C:\folder1\" (which is a different value than testPath). So change the code in the 2nd snippet portion to:
'add the start folder
stack.Push(New System.IO.DirectoryInfo(testPath))

And change the value of testPath so that it has an ending "\" character, so to "C:\MyPath\"
 

Latest posts

Back
Top