Solved

How to find text string in text file

Posted on 2003-12-08
7
626 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 76
Saving history changes to sub form 4 48
VBA filters 2 66
add criteria to query in VB, Access 2003 2 10
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

830 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