Link to home
Start Free TrialLog in
Avatar of Amirlit
Amirlit

asked on

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

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"
        Loop

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

Amir
Avatar of vinnyd79
vinnyd79

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
Loop
Close #ff
Close #tf
End Sub
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.
Avatar of Amirlit

ASKER

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

Amir
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"
ASKER CERTIFIED SOLUTION
Avatar of Dabas
Dabas
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial