search with more than one word

smiles

Well-known member
Joined
May 21, 2008
Messages
47
Programming Experience
Beginner
I type in textbox some words for searching, they are separated by "+"
(like: light + operation)
VB.NET:
Expand Collapse Copy
        Dim productName As String = productTxt.Text
        Dim searchWord As String() 
        searchWord = productName.Split(" + ") 
        Dim di As New IO.DirectoryInfo("E:\ScanImages")
        'Dim dirsTemp As DirectoryInfo()
        Dim dirs As DirectoryInfo() = di.GetDirectories("*" & searchWord(0) & "*", SearchOption.AllDirectories) 

        'If searchWord.Length > 0 Then
        'For index As Integer = 1 To searchWord.Length
        'dirsTemp = di.GetDirectories("*" & searchWord(index) & "*", SearchOption.AllDirectories)
        'Dim diNextTemp As DirectoryInfo
        'For Each diNextTemp In dirsTemp
        'dirs(dirs.Length) = diNextTemp
        'Next
        'Next
I think I have problem with the length of array, how can I combine the array of search results got from each word into one array ?
Thanks so much !!!
 
Better use a collection type for total results. AddRange is useful and easier than redimension the array and copying the elements yourself, well it's only two lines of code and a very basic operation but this is just one of many examples of how collections make such details trivial. So is the Distict extension method to remove duplicates, you could of course iterate all sub-results and check if the list contains the item before adding it, but this is another example of using the available tools to make coding easier and more readable.
VB.NET:
Expand Collapse Copy
Dim path As String = "C:\Program Files"
Dim searchwords() As String = {"Common", "Files"}
Dim list As New List(Of String)
For Each search In searchwords
    list.AddRange(IO.Directory.GetDirectories(path, String.Format("*{0}*", search), IO.SearchOption.AllDirectories))
Next
Dim results = list.Distinct
I changed it to string paths which makes it faster and easier to find distict matches.
 
Thanks so much, it works perfectly :)
VB.NET:
Expand Collapse Copy
    Private Sub SearchProduct()
        Dim productName As String = productTxt.Text
        Dim searchWord As String()
        searchWord = productName.Split(" + ")
        'Dim di As New IO.DirectoryInfo("E:\ScanImages")
        Dim path As String = "E:\ScanImages"
        Dim list As New List(Of String)
        For Each search In searchWord
            list.AddRange(IO.Directory.GetDirectories(path, String.Format("*{0}*", search), IO.SearchOption.AllDirectories))
        Next
        Dim results = list.Distinct
        'Dim dirsTemp As DirectoryInfo()
        'Dim dirs As DirectoryInfo() = di.GetDirectories("*" & searchWord(0) & "*", SearchOption.AllDirectories) 'new
        'If searchWord.Length > 0 Then
        'For index As Integer = 1 To searchWord.Length - 1
        'dirsTemp = di.GetDirectories("*" & searchWord(index) & "*", SearchOption.AllDirectories)
        'Dim u As Integer = dirs.Length
        'Dim v As Integer = dirsTemp.Length
        'Array.Resize(dirs, u + v)
        'Dim diNextTemp As DirectoryInfo
        'For Each diNextTemp In dirsTemp
        'Dim t As Integer = 0
        'dirs(u + t) = diNextTemp
        't = t + 1
        'Next
        'Next
        'End If
        'Dim diNext As DirectoryInfo
        Dim i As Integer = 0
        'For Each diNext In dirs
        For Each diNext As String In results
            'Dim j As Integer = 0
            Dim diNextReducedPath As String
            Dim value As String()
            'diNextReducedPath = Replace(diNext.FullName, "E:\ScanImages\", "")
            diNextReducedPath = Replace(diNext, "E:\ScanImages\", "")
            value = diNextReducedPath.Split(IO.Path.DirectorySeparatorChar)
            If value.Length = 3 Then
                TreeView1.Nodes.Add(New TreeNode(value(0)))
                TreeView1.Nodes(i).Nodes.Add(New TreeNode(value(1)))
                TreeView1.Nodes(i).Nodes(0).Nodes.Add(New TreeNode(value(2)))
            End If
            If value.Length = 2 Then
                TreeView1.Nodes.Add(New TreeNode(value(0)))
                TreeView1.Nodes(i).Nodes.Add(New TreeNode(value(1)))
            End If
            i = i + 1
        Next
    End Sub
 
Back
Top