Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract lines from a log file and create new one to email

Status
Not open for further replies.
Feb 29, 2012
26
GB
Hi

We have a log file. We would like to just email the file with , for example the last 10 lines of the file.

Does anyone know of a way to extract the lines and then create a new text file with only the lines.

Thanks

 
I use the code below to send emails.
Code:
Dim objEmail, 
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "fromaddress@domain.com"
objEmail.To = "email2@domain.com;email2@domain.com"
objEmail.Subject = "My subject"
objEmail.TextBody = "Use this line for a text-only message body." 
'objEmail.HTMLBody = "Use this line for an HTML message body."

objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = _
        "smtp.myemailserver.com" 
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

As far as extracting the last X lines of a log file, one way is to read the file to a variable, split by carriage returns (creating an array with one line per element, and storing only the last X lines.

Code:
Const ForReading = 1
Dim LinesFromEnd, sLogFile, sEmailBody 
LinesFromEnd = 10
sLogFile = "C:\log.txt"

Dim fso, f, arrLines
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(sLogFile, ForReading)
arrLines = Split(f.ReadAll, vbCrLf)
For x = (UBound(arrLines) - LinesFromEnd) To UBound(arrLines)
   sEmailBody = sEmailBody & arrLines(x) & vbCrLf
Next

'send email with sEmailBody as the body
Not tested, but should get you on your way
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top