Solved

look for a char string in a text file

Posted on 2006-11-30
6
219 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 62

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
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 …

939 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now