Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

13 Experts available now in Live!

Get 1:1 Help Now