?
Solved

How to find text string in text file

Posted on 2003-12-08
7
Medium Priority
?
632 Views
Last Modified: 2010-05-01
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

0
Comment
Question by:rvaldivia
7 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 400 total points
ID: 9898044
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9898070
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9898130
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:ictis
ID: 9900290
what wrong with using


if Not IsNumeric(sTest) then
    msgbox "This contains a string..."
end if
0
 

Author Comment

by:rvaldivia
ID: 9904214
Thanx a lot!!!
0
 

Expert Comment

by:dominica526
ID: 10068025
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
 
LVL 28

Expert Comment

by:vinnyd79
ID: 10068115
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question