Link to home
Start Free TrialLog in
Avatar of ECCSS
ECCSS

asked on

VBScript to Read Text file and Extract certain text then email it

i need a script to read a text file and extract lines that have specific words then email those lines.

An example is below of the information note the path and file that it is refering to updates regularly. So the key words it will be looking for in the script is Updated,New, Failed and Error.

This is the information I require in the email.
Updated: d:/ftp/roadmaps.zip
      Updated: d:/ftp/crscjac.zip
      Updated: d:/ftp/roadmaps.zip
      New: d:/ftp/sjairw.zip
      New: d:/ftp/roadmaps.zip
      Updated: d:/ftp/roadmaps.zip
Avatar of Shift-3
Shift-3
Flag of United States of America image

Paste the script below into a text file with a .vbs extension.  Customize the value of the strFile variable on line 4 with the location of the text file to read from.  Customize the contents of the arrKeyWords array on line 5 with the words to search for.  Customize the email variables on lines 7-10 with the desired values.

Running the script will extract any matching lines from the text file and email them to the specified address.  As written, the search is not case-sensitive.


Const ForReading = 1
Const TriStateUseDefault = -2
 
strFile = "c:\files\textfile.txt"
arrKeyWords = Array("Updated","New","Failed","Error")
 
strEmailFrom = "do.not.reply@example.com"
strEmailTo = "eccss@example.com"
strEmailSubject = "lines extracted from " & strFile
strSMTP = "smtpserver.example.com"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
arrLines = Split(objFile.ReadAll, vbCrLf)
objFile.Close
 
For Each strLine in arrLines
    For Each strKeyWord in arrKeyWords
        If InStr(1, strLine, strKeyWord, vbTextCompare) > 0 Then
            strEmailBody = strEmailBody & strLine & vbCrLf
        End If
    Next
Next
 
Set objEmail = CreateObject("CDO.Message")
 
objEmail.From = strEmailFrom
objEmail.To = strEmailTo
objEmail.Subject = strEmailSubject
objEmail.Textbody = strEmailBody
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTP
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
 
objEmail.Send

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial