Search for a value in a string and pipe out line in a text file

Posted on 2003-02-28
Medium Priority
Last Modified: 2010-05-01
I want to write an app that will search a text file line-by-line for the word error. And if it finds the word error to pipe it out to a text file.

Here is what I have

Dim sr As StreamReader = System.IO.File.OpenText("C:\err.log")
        Do Until sr.Peek = -1
            SearchString = sr.ReadLine              SearchChar = "Error"

But How do I tell it to find the searchChar in the sr.Readline?

Question by:Amirlit
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 28

Expert Comment

ID: 8044212
try this:

Private Sub Command1_Click()
Dim sFile As String
Dim errFile As String
Dim SearchChar As String, Ln As String
Dim ff As Integer, tf As Integer

SearchChar = "Error"
sFile = "C:\err.log"
errFile = "C:\errors.log"

ff = FreeFile
Open sFile For Input As #ff
tf = FreeFile
Open errFile For Output As #tf
Do Until EOF(ff)
Line Input #ff, Ln
If InStr(1, Ln, SearchChar) > 0 Then
    ' found "error"
    Print #tf, Ln
End If
Close #ff
Close #tf
End Sub
LVL 28

Expert Comment

ID: 8044223
In the above comment you might want to open errFile for append instead of output. If you open for append it will add to the file where opening for output will overwrite the file.

Author Comment

ID: 8044227
This is not working...It might help if I state that I am writing this in VB.NET

LVL 28

Expert Comment

ID: 8044254
Not sure about .net
You could try using Instr to find "Error" in your SearchString. It will return the position where the searchchar was found.If not found it will return 0.
Again,Im not sure if it will work in .net. Sorry.

if Instr(1,SearchString,SearchChar) > 0 Then MsgBox "Found"
LVL 27

Accepted Solution

Dabas earned 400 total points
ID: 8045085
I only installed Vb.Net on my system this week, but will attempt to help. Maybe we can learn from it together!
(You would have more chance to get a .Net expert if you had posted in the .Net section)

SearchChar = "Error"
Do Until sr.Peek = -1
     SearchString = sr.ReadLine              
     If Instr(1, SearchString, SeachChar) > 0 then
         'The SearchChar has been found, handle it here...

You will of course, have to open another StreamReader to create or append the error log and write to it

Hope this helps!


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Suggested Courses

762 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