Solved

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

Posted on 2009-07-01
2
1,886 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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