rvaldivia
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
what wrong with using
if Not IsNumeric(sTest) then
msgbox "This contains a string..."
end if
if Not IsNumeric(sTest) then
msgbox "This contains a string..."
end if
ASKER
Thanx a lot!!!
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
Thanks
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
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
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