Using the FIND Method in a TXT DOC.

Posted on 2007-08-01
Last Modified: 2012-05-05

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?

Question by:Mr_Fulano
    LVL 53

    Accepted Solution

    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

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

    Expert Comment

    Expounding Dhaest solution:

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

    Expert Comment

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

    Author Comment

    Thank you all !  I used Dhaest's solution and it worked very well for my needs.

    Thanks again,

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now