?
Solved

Using the FIND Method in a TXT DOC.

Posted on 2007-08-01
4
Medium Priority
?
197 Views
Last Modified: 2012-05-05
Hi,

I'm want to use the FIND Method (or something similar) to search a text document, which contains a list of words (one word per line).  I've been able to do what I want, if I put the list into a RichTextBox and use the following code:

myIndex= RichTextBox1.Find("string1")  '>> string1 is the word I'm searching for in the list.

This either sets the integer,  myIndex to  "-1", meaning it could not find what I was looking for, or it returns an integer (> 0) meaning it did find my string somewhere in the list.

However, how would I do that if I simply wanted to open a TXT document in memory and FIND the string I'm searching for right from that TXT document without loading its contents into a RTB (which I don't want to do)?

I assume I'd use Streamreader to open and read the document, but then how would I use the FIND command with Streamreader? -- Is there a better way?

Thanks,
Fulano
0
Comment
Question by:Mr_Fulano
4 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 19614824
sContents = GetFileContents("C:\test.txt")
if sContents.Contains("yourstring")

end if

Public Function GetFileContents(ByVal FullPath As String) As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Return strContents
        Catch Ex As Exception
           throw Ex
        End Try
    End Function
0
 
LVL 8

Expert Comment

by:harris_c
ID: 19614890
Expounding Dhaest solution:

            objReader = New StreamReader("C:\FileName").ReadToEnd.IndexOf ("StringToFind")
0
 
LVL 10

Expert Comment

by:ericwong27
ID: 19615697
If My.Computer.FileSystem.ReadAllText("C:\FileName").IndexOf("StringToFind") > -1 Then
' Do something
End If
0
 

Author Comment

by:Mr_Fulano
ID: 19622633
Thank you all !  I used Dhaest's solution and it worked very well for my needs.

Thanks again,
FDT
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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

809 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