We help IT Professionals succeed at work.

adding files to listboxes

Last Modified: 2013-11-26
I have a program that will search directories and add files to a listbox by the extension you enter. I have a table created in access that contains files. The program will check the database to see if files exist. I have it where if the path in tblExclude is equal to the path textbox1 then add files to listbox 2 and all other files are added to listbox1. My problem is I don't want the files that are added to listbox2 to be added to listbox1. The files that are in tblExclude are being added to both listboxes instead of just listbox2.  I know what my problem is but I don't know how to fix it.  In the code below I first create and fill an array with files that I want to exclude, fill listbox2 with the values from the array, and then at the end I fille listbox1 with files obtained in a completely different way.  Can someone please help me with this?  Here is the code below:

Private Sub GetDirectoryContents(ByVal dirs() As String, ByVal patterns() As String)
        'Declare variable.
        Dim dDir As DirectoryInfo

        'Search directory for files and add to the listbox.
        For Each sDir As String In dirs
            If Not Directory.Exists(sDir) Then Continue For
            dDir = New DirectoryInfo(sDir)
            For Each ext As String In patterns
                For Each fi As FileInfo In dDir.GetFileSystemInfos("*." & ext)
    End Sub

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        'Get the entered file paths and patterns
        Dim enteredFilePaths As String() = TextBox1.Text.Split(New String() {";"}, StringSplitOptions.RemoveEmptyEntries)
        Dim patterns As String() = TextBox2.Text.Split(New String() {";"}, StringSplitOptions.RemoveEmptyEntries)

        'Declare a string array to store the file paths that are not found in the database
        Dim filePaths() As String = Nothing
        Dim dv As DataView = dt.DefaultView
        dv.Sort = "ExcludePath ASC"

        For Each path As String In enteredFilePaths
            'Check to make sure the file path does not have "\" as it's last character.
            If path.LastIndexOf("\") = path.Length - 1 Then
                path = path.Substring(0, path.Length - 1)
            End If
            Dim rows As DataRowView() = dv.FindRows(path)
            If rows.Length > 0 Then
                'Only add the file if it is one of the entered extensions.
                For Each row As DataRowView In rows
                    Dim ext As String = System.IO.Path.GetExtension(row.Item("ExcludeFileName").ToString).Replace(".", "")
                    If Me.TextBox2.Text.IndexOf(ext, 0, StringComparison.CurrentCultureIgnoreCase) > -1 Then

                        'It was found in the database, display it in the listbox.
                    End If
                'It was not found in the database
                Dim i2 As Integer = 0
                If Not filePaths Is Nothing Then
                    i2 = UBound(filePaths) + 1
                End If

                'Resize the array
                ReDim filePaths(i2)

                'Add the path to the array
                filePaths(i2) = path
                End If
        GetDirectoryContents(enteredFilePaths, patterns)
    End Sub
Watch Question

For each file you find, before you add it to listbox1, iterate through listbox2 and see if the file exists:

Dim fFound As Boolean = False

For each item As ListItem in listbox2.Items
     If item.Value = "...The file you found..." Then
           fFound = True
     End If

If fFound = False Then
     'Add the item to listbox1
     'Reset fFound
     fFound = False
End If


Ok, how will I place this in my code because I can't get it to work?
This one is on us!
(Get your first solution completely free - no credit card required)

This will work if you've populated the "Value" field for each listitem in Listbox2 with the filenames.  If you've populated hte Text with the filename, then use:  If item.Text = fi.Name Then


I keep getting the following error:
Unable to cast object of type 'System.String' to type 'System.Windows.Forms.ListViewItem'.


Never mind I got it.  It works fine now.  Thanks for your help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.