Avatar of DavidSelby
DavidSelby
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Script to regularily check .txt document and send email if certain words appear

We have a service that writes updates into a log .txt file.

I was wondering how to

A schedule a script to run every hour to
B check for certain words, ie fail, shutdown, and
C email if those words appear.

I'm quite new to scripting so please explain at an easy level if possible.
VB ScriptScripting Languages

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
SOLUTION
Alan_White

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Alan_White

Actually, I've just knocked up a very quick script for "B".  As you can hopefully see, I've left out the email bit, it just echo's it to screen at the moment.

You need to specify the path in the cInputFile line and you can add or change the keywords with the dKeyWords dictionary.

Having knocked this up, it has made me wonder about your specification.  Is it enough to know that one of your keyword is somewhere is the file or do you want a nearby chuck on log inculded in the email?  How should the script handle subsequent passes? Multiple instances of the same keyword?

Option Explicit

Dim dKeyWords
Set dKeyWords = CreateObject("Scripting.Dictionary")
dKeyWords.add "error", ""
dKeyWords.add "fault", ""

Const cInputFile = "C:\dev\EEPollFileForText\inputfile.txt"
Const forReading = 1

Dim oFSO, oFile, sContentsOfFile, dictionaryItem

Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(cInputFile) Then
	Set oFile = oFSO.OpenTextFile(cInputFile, forReading)
	sContentsOfFile = oFile.ReadAll
	sContentsOfFile = LCase(sContentsOfFile)
	For Each dictionaryItem In dKeyWords
		If InStr(1,sContentsOfFile,LCase(dictionaryItem),vbTextCompare) <> 0 Then
			email "Found the word " & dictionaryItem & " in " & cInputFile
		End If
	Next
Else
	email "Input file not found!"
End If


Sub email(sText)
	WScript.Echo sText
End Sub

Open in new window

RobSampson

Hi, I've added my email sub to your code.

Regards,

Rob.

Option Explicit

' Email variables:
Dim strServer, strTo, strFrom, strSubject, strBody
strServer = "mailhost.abc.com"
strTo = "john.doe@abc.com"
strFrom = "john.doe@abc.com"
strSubject = "Subject Here"
strBody = "This is the body:" & vbCrLf

Dim dKeyWords
Set dKeyWords = CreateObject("Scripting.Dictionary")
dKeyWords.add "error", ""
dKeyWords.add "fault", ""

Const cInputFile = "C:\dev\EEPollFileForText\inputfile.txt"
Const forReading = 1

Dim oFSO, oFile, sContentsOfFile, dictionaryItem

Set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(cInputFile) Then
	Set oFile = oFSO.OpenTextFile(cInputFile, forReading)
	sContentsOfFile = oFile.ReadAll
	sContentsOfFile = LCase(sContentsOfFile)
	For Each dictionaryItem In dKeyWords
		If InStr(1,sContentsOfFile,LCase(dictionaryItem),vbTextCompare) <> 0 Then
			SendEmail strServer, strTo, strFrom, "Words found in text file", "Found the word " & dictionaryItem & " in " & cInputFile, ""
		End If
	Next
Else
	SendEmail strServer, strTo, strFrom, "Text file not found", oInputFile & " was not found!"
End If

Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
        Dim objMessage
        
        Set objMessage = CreateObject("CDO.Message")
        objMessage.To = strTo
        objMessage.From = strFrom
        objMessage.Subject = strSubject
        objMessage.TextBody = strBody
  		If strAttachment <> "" Then objMessage.AddAttachment strAttachment
  		
        '==This section provides the configuration information for the remote SMTP server.
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        'Name or IP of Remote SMTP Server
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
        'Server port (typically 25)
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25      
        objMessage.Configuration.Fields.Update
        '==End remote SMTP server configuration section==
 
        objMessage.Send
        Set objMessage = Nothing
End Sub

Open in new window

RobSampson

One thing you might want to do though, rather than email for *each* word found, is toggle a flag so that you can email only once if *any* word is found....let me know if you want that, and I'll adjust it for you.

Regards,

Rob.
Your help has saved me hundreds of hours of internet surfing.
fblack61
DavidSelby

ASKER
Hi Alan and Rob

Thanks for your reply, sorry its taken me a while to look at it, This is really helpful and definitly along the right lines

Our log gets automatically truncated when it gets long enough, and I dont wan't to interfere with that process. It happens every few days.

With that in mind is it possible to have the script check the timestamp for the line and compare that to the current system time so that its only looking at the last 2 hours?

timestamp format is: 2012-03-23 19:00:37.

If you guys can get it to do that, Im pretty sure that will meet my needs.

Many thanks

Dave
ASKER CERTIFIED SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Alan_White

Nice one Rob.  Beat me to it.  I'm only an expert Monday to Friday :-)
RobSampson

No problem. Yesterday was Monday for me, so I'm ahead of you!  It's bright and early Tuesday morning here now....the sun's not even up yet !!  How sad :-(
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.