Link to home
Start Free TrialLog in
Avatar of bmcnulty
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
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

if the number of spaces does not coincide with the text string, it will not be found

limit the search string to a determined format

strSearch = "Number of records processed"
Avatar of bmcnulty
bmcnulty

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.
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.
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
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
< strSearch = "Number of records processed"&string(9,chr(9))&"0" > will not work, since the number of Tab characters is undetermined
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
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.
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:
dim oRE as object
set oRE = createobject("vbscript.regexp")
oRE.Pattern ="Number of records processed\s+0"
If oRE.Test(textLine) Then

End If

Open in new window


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.