Solved

look for a char string in a text file

Posted on 2006-11-30
6
222 Views
Last Modified: 2010-04-23
I have a log file that is written every day and I would like to have a script to check it for certain entries.  Below is an example of the log:

11/30/2006 6:00:01 AM - Starting Script
11/30/2006 6:00:01 AM - All servers appear to be online
11/30/2006 6:00:08 AM - No reported pending CHKDSK for any defined servers
11/30/2006 6:00:08 AM - No disk space issues found on servers
11/30/2006 6:00:08 AM - All defined services on NWPSRV21 appear to be running.
11/30/2006 6:00:08 AM - All defined services on NWPSRV11 appear to be running.
11/30/2006 6:00:09 AM - All defined services on TWPSRV21 appear to be running.
11/30/2006 6:00:11 AM !- UWPSRV09 Radia Configuration Server is Stopped

I would like the script to read the log file and list the lines that have "!-" indicating an error.  How can I use streamreader to find only these lines and create them into a variable?  Chad
0
Comment
Question by:chadmanvb
6 Comments
 
LVL 10

Assisted Solution

by:Kinger247
Kinger247 earned 100 total points
ID: 18048877
hi chadmanvb, something like this ?  it doesn't use the streamreader, but the System.IO.File.ReadAllLines object.

        Dim arrLines() As String = System.IO.File.ReadAllLines("c:\test2.txt")
        Dim arrErrors(0) As String
        Dim Index As Integer = -1

        For Each strLine As String In arrLines
            If strLine.Contains("!-") Then
                Index = arrErrors.GetLength(0) + 1
                ReDim Preserve arrErrors(Index)
                arrErrors(Index) = strLine.Trim
            End If
        Next

        '//-- Show results
        For Each strLine As String In arrErrors
            Debug.WriteLine(strLine)
        Next
0
 
LVL 6

Accepted Solution

by:
riyazthad earned 200 total points
ID: 18048999
       Dim f As New IO.StreamReader("C:\file.txt")
        Dim line As String
        Dim Var As String

        Do
            line = f.ReadLine
            If InStr(line, "!") > 0 Then
                Var = line
            End If
        Loop Until line Is Nothing
        MessageBox.Show(Var)
        f.Close()
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 100 total points
ID: 18049179
Hi chadmanvb;

Here is a solution using Regular Expression. It uses a TextBox1 with the Multiline Property set to True and expand as needed.

Imports System.Text.RegularExpressions
Imports System.IO

        Dim LogFile As String = "C:\Temp\Error.log"  ' Log file location
        Dim sr As New StreamReader(LogFile )
        Dim input As String = sr.ReadToEnd()
        sr.Close()

        Dim mc As MatchCollection = Regex.Matches(input, "^(.*?!-.*)$", _
            RegexOptions.Multiline)
        For Each m As Match In mc
            TextBox1.AppendText(m.Groups(1).Value & vbCrLf)
        Next

Fernando
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:Mohamed Zedan
Mohamed Zedan earned 100 total points
ID: 18049545
and here is another version they are all great :)

Private Funtion GetErrorLines(strPath as string) as Collections.Specialized.StringCollection
            Dim sr As New IO.StreamReader(strPath)
            Dim line As String
            Dim Results As New Collections.Specialized.StringCollection

            While sr.Peek <> -1
                line = sr.ReadLine
                If line.IndexOf("!-") >= 0 Then
                    Results.Add(line)
                End If
            End While
            sr.Close()
            Return Results
End Function
0
 

Author Comment

by:chadmanvb
ID: 18056409
Great!  They all worked.  Chad
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 18056505
Thats the thing with .net, there's 1001 ways of doing the same task :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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