How to find text string in text file

Hi,
I need a function that returns true when a text string is found in a text file. Something like:

Function Find(stFile As String, stText As String) as Boolean
.
.
End Function

Thanx in advance for your help

rvaldiviaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
Function Find(stFile As String, stText As String) As Boolean
Dim ff As Integer
Dim Ln As String
ff = FreeFile
Open stFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
If InStr(Ln, stText) > 0 Then
    Find = True
    Close #ff
    Exit Function
End If
Loop
Close #ff
Find = False
End Function


Private Sub Command1_Click()
MsgBox Find("C:\Test.txt", "text to find")
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vinnyd79Commented:
If you do not want to find to be case sensative you can use:

Function Find(stFile As String, stText As String) As Boolean
Dim ff As Integer
Dim Ln As String
ff = FreeFile
stText = LCase(stText)
Open stFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
If InStr(LCase(Ln), stText) > 0 Then
    Find = True
    Close #ff
    Exit Function
End If
Loop
Close #ff
Find = False
End Function


Private Sub Command1_Click()
MsgBox Find("C:\Test.txt", "Text to Find")
End Sub
0
EDDYKTCommented:
Or

Function Find(stFile As String, stText As String) As Boolean
Dim ff As Integer,  s As String
   
ff = FreeFile
Open stFile For Binary Access Read Lock Read Write As #ff
s = Space$(LOF(ff))
Get #ff, , s
Close ff
Find = iff(instr(s, stText)>0, true, false)
end sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ictisCommented:
what wrong with using


if Not IsNumeric(sTest) then
    msgbox "This contains a string..."
end if
0
rvaldiviaAuthor Commented:
Thanx a lot!!!
0
dominica526Commented:
This was really great code, it was really simple and works like a charm. Is it possible to get the line number where the text was found? I need to get the all the data in the next 5 lines. In my situation the text file changes everyday so the string that I am looking for will always be in a different location. If I had the line number I could simply read the next five lines into my report.

Thanks
0
vinnyd79Commented:
If you are referring to my example you could count the lines and have it return a string rather than a boolean:

Function Find(stFile As String, stText As String) As String
Dim ff As Integer
Dim Ln As String
Dim LineNum As Integer
LineNum = 0
ff = FreeFile
Open stFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
LineNum = LineNum + 1
If InStr(Ln, stText) > 0 Then
    Find = "Found On Line # " & LineNum
    Close #ff
    Exit Function
End If
Loop
Close #ff
Find = "Not Found"
End Function

Private Sub Command1_Click()
MsgBox Find("C:\Test.txt", "Text to Find")
End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.