Solved

look for a char string in a text file

Posted on 2006-11-30
6
218 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
Comment Utility
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
Comment Utility
       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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Assisted Solution

by:Mohamed Zedan
Mohamed Zedan earned 100 total points
Comment Utility
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
Comment Utility
Great!  They all worked.  Chad
0
 
LVL 10

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

7 Experts available now in Live!

Get 1:1 Help Now