Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

look for a char string in a text file

Posted on 2006-11-30
6
Medium Priority
?
226 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 400 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 800 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Assisted Solution

by:Mohamed Zedan
Mohamed Zedan earned 400 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

971 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