bmcnulty
asked on
Search for text in a .txt file
I am trying to search through all text files in a folder for a specific string of text. i started with just trying the following code on just the one file. If i put "True" in the text file it works fine, i think the issue has to do with the spaces in the data in the text file. Attached is the format the text files will be in.
Dim stDestFile As String, stDataLine As String
Dim textLine As String, strSearch As String
stDestFile = "C:\test\test.txt"
strSearch = "Number of records processed 0"
Open stDestFile For Input As #1
Do While Not EOF(1)
Line Input #1, textLine
If InStr(textLine, strSearch) <> 0 Then
'MsgBox "Found " & strSearch
Close #1
Exit Function
End If
Loop
Close #1
Test.txt
Dim stDestFile As String, stDataLine As String
Dim textLine As String, strSearch As String
stDestFile = "C:\test\test.txt"
strSearch = "Number of records processed 0"
Open stDestFile For Input As #1
Do While Not EOF(1)
Line Input #1, textLine
If InStr(textLine, strSearch) <> 0 Then
'MsgBox "Found " & strSearch
Close #1
Exit Function
End If
Loop
Close #1
Test.txt
ASKER
I copied the text directly from the file, but it does not seem to match when searching, i need to look for
"Number of records processed 0" to trigger my alerts.
"Number of records processed 0" to trigger my alerts.
make sure you are comparing the text exactly (upper lowercase etc), the number of spaces between words, if possible attach a sample text file and I will look into it.
ASKER
test.txt is attached above, ill keep trying different spacing in my code as well.
the text in the test file have characters other than spaces
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
< strSearch = "Number of records processed"&string(9,chr(9) )&"0" > will not work, since the number of Tab characters is undetermined
ASKER
Both of these work, setting strSearch = "Number of records processed"&string(2,chr(9))&"0" will find the text, assuming the file is always generated that way.
is not undetermined in the sample file there are 2 of them exactly
ASKER
Arana is correct, (2,chr(9)) will find the text i am looking for. I do like the idea of stripping all the spaces(tabs) out before searching..may be more reliable if this report changes around at all.
Thanks all.
Thanks all.
it is up to you to determine if you want to match the file exactly or if you want to find the text weather it has spaces or tabs or nothing in between (if you use the replacing code it would also match "Numberofrecordsprocessed0 ". or a mix between tabs and spaces ie not an exact match.
< stripping all the spaces(tabs) out before searching..may be more reliable > Indeed.
1. You can use the regular expression object to do your comparison.
Example:
2. You would get better performance if you read the entire file into a string variable, rather than doing line-at-a-time reads. Of course, you wouldn't be able to do this with very very large files due to memory constraints.
3. You might be able to let the operating system do that search for you like it does in the explorer application.
Example:
dim oRE as object
set oRE = createobject("vbscript.regexp")
oRE.Pattern ="Number of records processed\s+0"
If oRE.Test(textLine) Then
End If
2. You would get better performance if you read the entire file into a string variable, rather than doing line-at-a-time reads. Of course, you wouldn't be able to do this with very very large files due to memory constraints.
3. You might be able to let the operating system do that search for you like it does in the explorer application.
limit the search string to a determined format
strSearch = "Number of records processed"