Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSearch files in a directory hierarchy for a target string in Visual Basic .NET
DescriptionThis example shows how to search files in a directory hierarchy for a target string in Visual Basic .NET.
Keywordsexample program,search file contents, Visual Basic, Visual Basic .NET, VB.NET
CategoriesFiles and Directories, Windows
 
I've been using Windows 7 for a couple of weeks and one of the things I find most annoying (in Vista, too) is that it doesn't search files properly. Despite setting this indexing setting and adding that file extension to whatever list, it just won't search inside the files I want it to such as C# program files with a .cs extension. (It has become a long-standing joke that Microsoft can't search files and probably never will.)

So in frustration I wrote my own file searcher.

The following code shows the program's key pieces.

 
' If we launched from the Send To menu,
' use the argument as our directory.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    If My.Application.CommandLineArgs.Count > 0 Then
        txtDirectory.Text = _
            My.Application.CommandLineArgs(0)
        txtTarget.Focus()
    End If
End Sub

' Search.
Private Sub btnSearch_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSearch.Click
    lstResults.Items.Clear()
    Dim dir_info As New DirectoryInfo(txtDirectory.Text)

    ListFiles(lstResults, cboPattern.Text, dir_info, _
        txtTarget.Text)

    Beep()
End Sub

' Add the files in this directory's subtree 
' that match the pattern to the ListBox.
Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern _
    As String, ByVal dir_info As DirectoryInfo, ByVal _
    target As String)
    ' Get the files in this directory.
    Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
    For Each fs_info As FileInfo In fs_infos
        If target.Length = 0 Then
            lstResults.Items.Add(fs_info.FullName)
        Else
            Dim txt As String = _
                File.ReadAllText(fs_info.FullName)
            If txt.IndexOf(target, _
                StringComparison.OrdinalIgnoreCase) >= 0 _
                Then
                lstResults.Items.Add(fs_info.FullName)
            End If
        End If
    Next fs_info
    fs_infos = Nothing

    ' Search subdirectories.
    Dim subdirs() As DirectoryInfo = _
        dir_info.GetDirectories()
    For Each subdir As DirectoryInfo In subdirs
        ListFiles(lst, pattern, subdir, target)
    Next subdir
End Sub
 
When the program starts, it checks its command line arguments. The first one is always the name of the executing program. If there is a second argument, then it should be the name of a directory dragged onto the executable or sent to the program via the Send To menu so the program enters it in the Directory TextBox.

(To make using this program easy, I added it to my Send To menu. Then I can right-click a directory, open the Send To menu, and select this program to make it search the directory. For more information on adding items to the Send To menu, see Add items to the "Send To" menu.)

When you click the Search button, the program clears its results list, makes a DirectoryInfo object representing the start directory, and calls the ListFiles function.

ListFiles uses the DirectoryInfo object's GetFiles method to get a list of files that match the file pattern. If there is no target string to look for, the program simply adds the files to the result list.

If there is a target string, the program uses System.IO.File.ReadAllText to read the file's contents into a string. If the target is in the file, the program adds the file to the list.

After searching the files in this directory, the program uses the DirectoryInfo object's GetDirectories method to get a list of its subdirectories. The program calls ListFiles recursively to search each subdirectory.

The result isn't as fancy as Windows Explorer's search. You can't search by file date, size, author, rating, or any of the other zillion things you would never want to use in a search.

However, it works and can search any kind of file. It's even a lot faster than Windows' search. Now if Microsoft would just get a clue. Perhaps they can include a working search tool in the next version of Windows.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated