Solved

How to find text string in text file

Posted on 2003-12-08
7
627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 100 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

615 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