• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

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
0
Amirlit
Asked:
Amirlit
  • 3
1 Solution
 
vinnyd79Commented:
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
0
 
vinnyd79Commented:
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.
0
 
AmirlitAuthor Commented:
This is not working...It might help if I state that I am writing this in VB.NET

Amir
0
 
vinnyd79Commented:
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"
0
 
DabasCommented:
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...
     endif
Loop

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

Hope this helps!

Dabas
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now