Solved

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

Posted on 2009-07-01
2
1,878 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:ECCSS
  • 2
2 Comments
 
LVL 38

Expert Comment

by:Shift-3
ID: 24754356
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

0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 250 total points
ID: 24755583
Minor correction.


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
            Exit For
        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

0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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