|
-
Jul 1st, 2008, 10:44 AM
#1
Thread Starter
Lively Member
search for a text file in the sytem and search for the string in the text file
i want to search for a text file called 'data.txt' in the system...and search for a string called 'hello world' in the file...the below code retrieves the first path of the file.. from this path i have to get the text file and search for the string
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long
Public Function SearchAllDrives(pstrFile As String) As String
Dim i As Long
Dim strDrives() As String
Dim strMatch As String
For i = 1 To EnumerateDrives(strDrives)
strMatch = SearchDriveForFile(pstrFile, strDrives(i))
If Len(strMatch) <> 0 Then
SearchAllDrives = strMatch
Exit Function
End If
Next
End Function
' Populate a 1-base array with drive designations
' for all drive types. Return number of drives found.
Public Function EnumerateDrives(pstrDrives() As String) As Long
Dim i As Long
Dim strBuffer As String
Dim strDrive As String
ReDim pstrDrives(1 To 1)
strBuffer = Space$(255)
strBuffer = Left$(strBuffer, GetLogicalDriveStrings(255, ByVal strBuffer))
Do While InStr(strBuffer, "\")
strDrive = Left$(strBuffer, InStr(strBuffer, "\") - 1)
i = i + 1
ReDim Preserve pstrDrives(1 To i)
pstrDrives(i) = strDrive
strBuffer = Mid$(strBuffer, Len(strDrive) + 3)
Loop
If pstrDrives(1) = "" Then
Erase pstrDrives
EnumerateDrives = 0
Else
EnumerateDrives = i
End If
End Function
Public Function SearchDriveForFile(pstrFile As String, Optional ByVal pstrDrive As String = "C:") As String
Const MAX_PATH = 260
Dim strPath As String
Dim lngReturn As Long
strPath = String(MAX_PATH, 0)
pstrDrive = Left$(pstrDrive, 1) & ":\"
If SearchTreeForFile(pstrDrive, pstrFile, strPath) <> 0 Then SearchDriveForFile = Left$(strPath, InStr(1, strPath, Chr$(0)) - 1)
End Function
Last edited by Sgiri1; Jul 1st, 2008 at 11:08 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|