Solved

look for a char string in a text file

Posted on 2006-11-30
6
220 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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