Results 1 to 4 of 4

Thread: [RESOLVED] Search String Array by Multiple Words

  1. #1

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Resolved [RESOLVED] Search String Array by Multiple Words

    I have a function that returns files in a directory and sub-directories based on a search word. What I cant seem to figure out is how to return the file name if ONLY two or three words match. Meaning if my search words are curb;granite only the files that contain BOTH words should return.

    I'm sure I am missing something simple but its been a long week already. LOL

    Also if anyone has a completely different of better way to do this, I am all ears. The current goal is to populate a listbox with the file names for proof of concept. I eventually want to add them to a data grid view splitting the strings into columns. I.E. Filename, Root Folder, Sub Folder, Sub Sub Folder. LOL

    Here is what I have already...

    Code:
        Private Function FindFiles(ByVal searchWords As String) As List(Of String)
    
            Dim foundFiles As New List(Of String)()
    
            Dim searchWordArray() As String = searchWords.Split(";"c)
    
            Dim files As String() = Directory.GetFiles(_directory, "*.pdf", SearchOption.AllDirectories)
    
            For Each searchWord As String In searchWordArray
    
                For Each file In files
    
                    If file.ToLower.Contains(searchWord) Then
    
                        If Not foundFiles.Contains(file) Then foundFiles.Add(file)
    
                    End If
    
                Next
    
            Next
    
            foundFiles.Sort()
    
            Return foundFiles
    
        End Function

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Search String Array by Multiple Words

    Change the order of the loops, so that for each file you can check how many words match.

    eg:
    Code:
            For Each file In files
                Dim foundWords = 0
                For Each searchWord As String In searchWordArray
                    If file.ToLower.Contains(searchWord) Then
                        foundWords += 1
                    End If
                Next
                If foundWords = searchWordArray.Count Then foundFiles.Add(file)
            Next

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Search String Array by Multiple Words

    You could use multiple LINQ statements:
    Code:
    files.Where(Function(file) searchWordArray.Count(Function(search) file.Contains(search)) > 1)
    Example: https://dotnetfiddle.net/B6T3iJ

    This returns files where the number of search words appear in the filename more than once. If you want to modify the comparison then you'll need to do so (e.g. more than 1 but less than 4).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Addicted Member dprontnicki's Avatar
    Join Date
    Sep 2016
    Posts
    151

    Re: Search String Array by Multiple Words

    si_the_geek,

    Thank you so much! I see what your doing. Keep count/track of the found words from the search array in a file and then if the count of found words matches the array then add it to the list of strings.

    Brilliant, thank you again!

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