Home All Groups Group Topic Archive Search About
Author
16 Jan 2007 9:48 AM
Bengyke
Hi,

We have a lot of log files in txt format. How can I write a script that will
search in de text file for a certain text ? So if the script found that
messag (for example error ..) I can send an email or give a print-out.

Thanks !

Author
16 Jan 2007 10:14 AM
Andrew Watt [MVP]
Hi,

If you were using Windows PowerShell you could do the core of what you
want using the get-content cmdlet.

Suppose you have a file MyLog.txt. The following would display lines
on screen where "Error" occurs.

get-content MyLog.txt |
where-object {$_ -match "Error"}

If you wanted to save error lines to a text file use this:

$a = get-content MyLog.txt |
where-object {$_ -match "Error"}
$a > ErrorLines.txt

To retrieve the content of the ErrorLines.txt:

get-content ErrorLines.txt

Andrew Watt MVP

On Tue, 16 Jan 2007 01:48:01 -0800, Bengyke
<Beng***@discussions.microsoft.com> wrote:

Show quoteHide quote
>Hi,
>
>We have a lot of log files in txt format. How can I write a script that will
>search in de text file for a certain text ? So if the script found that
>messag (for example error ..) I can send an email or give a print-out.
>
>Thanks !
Author
16 Jan 2007 8:28 PM
urkec
Const ForReading = 1
fName = "C:\scripts\log.txt"
strToFind = "error"

Set fso = CreateObject ("Scripting.FileSystemObject")
Set file = fso.OpenTextFile (fName, ForReading, false)

If Not file.AtEndOfStream Then
    contents = file.ReadAll
Else
    Wscript.Echo "Log file empty"
    Wscript.Quit
End If

If InStr (contents, strToFind) > 0 Then
    'actions if error message found
End If
--
urkec


Show quoteHide quote
"Bengyke" wrote:

> Hi,
>
> We have a lot of log files in txt format. How can I write a script that will
> search in de text file for a certain text ? So if the script found that
> messag (for example error ..) I can send an email or give a print-out.
>
> Thanks !