Home All Groups Group Topic Archive Search About
Author
12 Feb 2006 7:36 PM
ari
hey all,

is there a way to search an entire text file for a string and if it finds
it, read the entire line it was found on?

thanks,
ari

Author
12 Feb 2006 8:42 PM
Chris
ari wrote:
> hey all,
>
> is there a way to search an entire text file for a string and if it finds
> it, read the entire line it was found on?
>
> thanks,
> ari


Do while FileStream.Peek <> -1
    Dim Line as sTring = FileStream.Readline
    if Line.Indexof(StringToFind) <> -1 then exit loop
Loop


Chris
Author
12 Feb 2006 8:45 PM
Sonu Kapoor
You can read the entire file via:

Dim sr as New StreamReader("C:\SomeDir\SomeFile.txt")
Dim contents as String
contents = sr.ReadToEnd()

Then you could check if contents contains the word that you are looking for i.e:

If contents.indexOf("search me") <> -1
  ' Read the file line by line
End if

However there isnt a way to read then just the line, you will basically have to read the entire file line by line (check the ReadLine method).

Hope it helps.

Sonu Kapoor [MVP]
W: www.DotNetSlackers.com
B: www.KapoorSolutions.com

---
Posted via www.DotNetSlackers.com
Author
12 Feb 2006 9:25 PM
Cerebrus99
Hi Ari,

You can easily use Regular Expressions to do this, too. I'm not sure,
but it may be more efficient than reading 1 character at a time.The
Following
example searches for the text "the more you forget" in a text file called
"Logic.txt",
containing the following 4 lines :
=============================
The more you learn, the more you know,
The more you know, the more you forget,
The more you forget, the less you know
So.. why learn.
=============================
It then outputs just the Line which contained the text you needed to search
for.

The sample code is :
---------------------------------

'Get the contents of the text file in a String variable.
Dim fs As New FileStream("..\Logic.txt", FileMode.Open, FileAccess.Read)
Dim sr As New StreamReader(fs)
Dim Contents As String = sr.ReadToEnd()

' Store the string you want to search for using a Regular expression. The
caret indicates the start of the line.
' (.*?) can match any text. Each set of enclosing brackets indicates 1 group
within the matched line.
Dim strSearch As String = "^(.*?)(the more you know,)"

' Create a Regex Object with this Regular expression. It is essential to set
the option "Multiline" in this case,
' which will give the intended meaning to the Caret.
Dim myRegex As New Regex(strSearch, RegexOptions.Multiline)

If myRegex.IsMatch(Contents) Then
  'The match succeeded. Get the matched text. The whole Match instance is
equivalent to Match.Groups(0).
  Dim myMatch As Match = myRegex.Match(Contents)
  Dim theFoundLine As String = myMatch.Groups(0).Value

  'Display the found line
  MsgBox(theFoundLine)

  'Optionally get the Length of the match and it's position in the input
string.
  MsgBox(myMatch.Length)
  MsgBox(myMatch.Index)
Else
  MsgBox("Failed")
End If

---------------------------------

Hope that should help.

Chris wrote :
> Do while FileStream.Peek <> -1
> Dim Line as sTring = FileStream.Readline
> if Line.Indexof(StringToFind) <> -1 then exit loop
> Loop

You probably made a few typos, Chris. The Peek method is only available to
the StreamReader class, not the FileStream class, as is the ReadLine method.
Finally, it's Exit Do and not Exit Loop ;-)

Warm Regards,

Cerebrus.
Author
12 Feb 2006 10:25 PM
Herfried K. Wagner [MVP]
"ari" <a**@discussions.microsoft.com> schrieb:
> is there a way to search an entire text file for a string and if it finds
> it, read the entire line it was found on?

Reading a text file line-by-line or blockwise with a progress indicator
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=readfile&lang=en>

+ 'InStr'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
13 Feb 2006 12:41 AM
ari
thanks everyone for the replies, this helped greatly.

ari

Show quoteHide quote
"ari" wrote:

> hey all,
>
> is there a way to search an entire text file for a string and if it finds
> it, read the entire line it was found on?
>
> thanks,
> ari