Searching a Wite file (Word pad RTF)

TexG

Well-known member
Joined
Apr 2, 2003
Messages
88
Location
San Antonio Texas
Hello all,

Working on a program to search a write file.

Im not sure on how to go about doing this.

Is the StreamReader the way to go and if so how?

What im trying to do is set it up so i can type in a textbox a word to search for. If the word is found then msgbox found word.

Any ideals on this?


Thanks
 
You might consider using a RichTextBox class to load the RTF file in;
then it will be just a matter of searching the Text property.
Code:
Dim rtb As New RichTextBox()
rtb.LoadFile("C:\SomeFile.txt")

If rtb.Text.IndexOf("Search For This!") > -1 Then
    MessageBox.Show("Found")
End If
 
VolteFace,

i have over 1200 files to search for a word.

I wanting to serch the file for that word first then im going to output a found this many words in this file. then go from there.

but the above works for another program im working on.

Any ideals?

thanks
 
Well, you can use a loop and keep searching the RTB text for that
word, each time starting at the previously found instance.

So, for example,
Code:
Dim position As Integer
Dim count As Integer

position = myText.IndexOf("moo")
Do Until position = -1
  count += 1
  position = myText.IndexOf("moo", position + ("moo").Length)
Loop
It is untested and is probably a bit flawed, but you should
get the idea. Once the loop completes, count will contain the number
of times the string was found.

Normally I would recommand that you use the Split method
of a String (using the word to find as delimeter), and checking the Length
of the returned array, but it seems the Split method in .NET only accepts
Char (single characters) as a delimeter, so this way is just
as good I think.


Loop
 
Back
Top