|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
reading text fileshey 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 ari wrote:
> hey all, Do while FileStream.Peek <> -1> > 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 Dim Line as sTring = FileStream.Readline if Line.Indexof(StringToFind) <> -1 then exit loop Loop Chris 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 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 You probably made a few typos, Chris. The Peek method is only available to> Dim Line as sTring = FileStream.Readline > if Line.Indexof(StringToFind) <> -1 then exit loop > Loop the StreamReader class, not the FileStream class, as is the ReadLine method. Finally, it's Exit Do and not Exit Loop ;-) Warm Regards, Cerebrus. "ari" <a**@discussions.microsoft.com> schrieb: Reading a text file line-by-line or blockwise with a progress indicator> 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? <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/> 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
Is there a difference between passing "" and passing Nothing to a Windows API ?
Moving a project from 2003 to 2005 Time? UI Design question 2003 0r 2005 Using GetOleDbSchemaTable and Visual Basic .NET for MS-Access and SQL Server Express TCP/IP Socket communication from multiple clients Need help with LastIndexOf Help to Italy HREF QUESTION (cross) |
|||||||||||||||||||||||