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
 
 
 
 
 
TitleFind files matching a pattern below a directory that do or don't contain a string
Keywordssearch, match, subdirectory
CategoriesUtilities
 
This program uses the FindAssociatedProgram function to find the application used to open .TXT files. Later if the user double clicks on a listed file, the program uses this program to open the file.
 
' Return the path to the program associated with this
' extension.
Private Function FindAssociatedProgram(ByVal extension As _
    String) As String
Dim temp_title As String
Dim temp_path As String
Dim fnum As Integer
Dim result As String
Dim pos As Integer

    ' Get a temporary file name with this extension.
    GetTempFile extension, temp_path, temp_title

    ' Make the file.
    fnum = FreeFile
    Open temp_path & temp_title For Output As fnum
    Close fnum

    ' Get the associated executable.
    result = Space$(1024)
    FindExecutable temp_title, temp_path, result
    pos = InStr(result, Chr$(0))
    FindAssociatedProgram = Left$(result, pos - 1)

    ' Delete the temporary file.
    Kill temp_path & temp_title
End Function
 
The program uses drive, directory, and file list controls to let the user select files in a directory. A ComboBox lets the user select a file filter or enter one of his own.

When the user clicks Search, the program uses the Split function to make an array containing the file filter's individual filters (i.e. if the filter is *.htm;*.html then the array contains *.htm and *.html in separate entries). For each filter in the array, the program then calls subroutine CheckFiles to examine files matching the pattern.

CheckFiles uses the FindFirstFile, FindNextFile, and FindClose API functions to search the directories beneath the one selected for files matching a pattern. For each file, it calls subroutine TargetInFile to see if the target string is in the file. CheckFiles XORs the result of TargetInFile and a Boolean indicating whether the user wants to find files containing the text or files not containing the text. That lets the program pick files where either:

  • The file contains the text and we don't want those without the text
  • The file doesn't contain the text and we do want those without the text

TargetInFile opens the file and reads it into a string. It then uses InStr to see if the target string is present.

 
' Search the selected files.
Private Sub cmdSearch_Click()
Dim pat As String
Dim p1 As Integer
Dim p2 As Integer
Dim patterns As Variant
Dim start_dir As String
Dim i As Integer
Dim count_missing As Boolean

    Screen.MousePointer = vbHourglass
    DoEvents

    ' Copy the patterns into an array.
    pat = cboPattern.Text
    p1 = InStr(pat, "(")
    If p1 > 0 Then
        p2 = InStr(pat, ")")
        pat = Mid$(pat, p1 + 1, p2 - p1 - 1)
    End If
    patterns = Split(pat, ";")

    ' Search the files.
    lstFiles.Clear
    start_dir = dirSearch.Path
    If Right$(start_dir, 1) <> "\" Then start_dir = _
        start_dir & "\"
    count_missing = (chkNotInFile.Value = vbChecked)
    For i = LBound(patterns) To UBound(patterns)
        CheckFiles start_dir, patterns(i), txtString.Text, _
            count_missing
    Next i

    Screen.MousePointer = vbDefault
End Sub

' Search all files below the directory that match the
' pattern.
Private Sub CheckFiles(ByVal start_dir As String, ByVal _
    Pattern As String, ByVal target As String, ByVal _
    count_missing As Boolean)
Const MAXDWORD = 2 ^ 32

Dim dir_names() As String
Dim num_dirs As Integer
Dim i As Integer
Dim fname As String
Dim new_files As String
Dim attr As Integer
Dim search_handle As Long
Dim file_data As WIN32_FIND_DATA
Dim file_size As Double

    ' Get the matching files in this directory.
    ' Get the first file.
    search_handle = FindFirstFile( _
        start_dir & Pattern, file_data)
    If search_handle <> INVALID_HANDLE_VALUE Then
        ' Get the rest of the files.
        Do
            fname = file_data.cFileName
            fname = Left$(fname, InStr(fname, Chr$(0)) - 1)
            If fname <> "." And fname <> ".." Then
                fname = start_dir & fname
                ' See if the target is in the file XOR
                ' we want files without the target.
                If TargetInFile(fname, target) Xor _
                    count_missing Then
                    lstFiles.AddItem fname
                    lstFiles.Refresh
                End If
            End If

            ' Get the next file.
            If FindNextFile(search_handle, file_data) = 0 _
                Then Exit Do
        Loop

        ' Close the file search hanlde.
        FindClose search_handle
    End If

    ' Optionally display the new list here.

    ' Get the list of subdirectories.
    search_handle = FindFirstFile( _
        start_dir & "*.*", file_data)
    If search_handle <> INVALID_HANDLE_VALUE Then
        ' Get the rest of the files.
        Do
            If file_data.dwFileAttributes And DDL_DIRECTORY _
                Then
                fname = file_data.cFileName
                fname = Left$(fname, InStr(fname, Chr$(0)) _
                    - 1)
                If fname <> "." And fname <> ".." Then
                    num_dirs = num_dirs + 1
                    ReDim Preserve dir_names(1 To num_dirs)
                    dir_names(num_dirs) = fname
                End If
            End If
            
            ' Get the next file.
            If FindNextFile(search_handle, file_data) = 0 _
                Then Exit Do
        Loop

        ' Close the file search handle.
        FindClose search_handle
    End If

    ' Search the subdirectories.
    For i = 1 To num_dirs
        CheckFiles start_dir & dir_names(i) & "\", Pattern, _
            target, count_missing
    Next i
End Sub

' Return True if the target is in the file.
Private Function TargetInFile(ByVal fname As String, ByVal _
    target As String) As Boolean
Dim fnum As Integer
Dim txt As String

    On Error GoTo OpenError
    fnum = FreeFile
    Open fname For Input As fnum
    txt = Input$(LOF(fnum), #fnum)
    Close fnum
    TargetInFile = (InStr(txt, target) > 0)
    Exit Function

OpenError:
    Exit Function
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated