Find the program associated with .txt files so we can open them later.
Use the FindFirstFile, FindNextFile, and FindClose API functions to find the files. Open the files, read them, and use InStr to search for the target string. List any files that contain it in a ListBox.
When the user double clicks a file in the ListBox, execute the program associated with .txt files passing it the name of the selected file as a parameter. If all is well, the associated program should 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
' 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)
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 = start_dir & Left$(fname, InStr(fname, _
Chr$(0)) - 1)
If TargetInFile(fname, target) Then
lstFiles.AddItem fname
lstFiles.Refresh
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
Next i
End Sub
' Return True if the target is in the file.
Private Function TargetInFile(fname, target) 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
|