Solved

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

Posted on 2009-07-01
2
1,867 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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now