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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find string in file from today yesterday's date

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Hi,
I have a log file I must look through each morning to see if there were any failures the previous day. Here's a small sample
Code:
1/16/2013 1:40:00 PM | Starting process ==========================================================
1/16/2013 1:40:00 PM | Import ERP order email data...
1/16/2013 1:40:01 PM | Imported Record Count = 0
1/16/2013 1:40:01 PM | Task Time: 0.4992 seconds
1/16/2013 1:40:01 PM | Import ERP order email data...
1/16/2013 1:40:01 PM | Imported Record Count = 0
1/16/2013 1:40:01 PM | Task Time: 0.078 seconds
1/16/2013 1:40:01 PM | Pre-validate email addresses ...
1/16/2013 1:40:01 PM | Invalid Email Count = 0
1/16/2013 1:40:01 PM | Task Time: 0.3432 seconds
1/16/2013 1:40:01 PM | Check order's ship date freshness...
1/16/2013 1:40:01 PM | Unfresh Record Count = 0
1/16/2013 1:40:01 PM | Task Time: 0.0468 seconds
1/16/2013 1:40:01 PM | Re-checking delivery records that have a Soft-Bounce status..
1/16/2013 1:40:04 PM | !!Unknown delivery results returned; ELSE in RecheckBouncedDeliveryResults() called.
1/16/2013 1:40:04 PM | Delivery: Del ID=aa5rc6c1-ss7f-23ef-a458-dac848876rd1 | Del Status=sent | Del Type=transactional
1/16/2013 1:40:04 PM | !!Unknown delivery results returned; ELSE in RecheckBouncedDeliveryResults() called.
1/16/2013 1:40:04 PM | Delivery: Del ID=9idj925-10pp-4def-a3dd-cca4a8479200 | Del Status=sent | Del Type=transactional
1/16/2013 1:40:04 PM | Re-checked delivery count: 4
1/16/2013 1:40:04 PM | Task Time: 2.7768 seconds
1/16/2013 1:40:04 PM | Get delivery results from Server...
5/25/2013 1:40:39 PM | New contacts count: 0
5/25/2013 1:40:39 PM | Task Time: 33.9612 seconds
5/25/2013 1:40:39 PM | Sending emails via Server system ...
5/25/2013 1:40:40 PM | An error occurred while receiving the HTTP response to [URL unfurl="true"]https://api.Server.com[/URL] This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
5/25/2013 1:40:40 PM | Email count sent for Server delivery: 0
5/25/2013 1:40:40 PM | Task Time: 0.546 seconds
5/25/2013 1:40:40 PM | Total Elapsed Time: 39.624 seconds
5/26/2013 1:40:40 PM | Process finished.
5/26/2013 1:40:39 PM | New contacts count: 0
5/26/2013 1:40:39 PM | Task Time: 33.9612 seconds
5/26/2013 1:40:39 PM | Sending emails via Server system ...
5/26/2013 1:40:40 PM | An error occurred while receiving the HTTP response to [URL unfurl="true"]https://api.Server.com[/URL] This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
5/26/2013 1:40:40 PM | Email count sent for Server delivery: 0
5/26/2013 1:40:40 PM | Task Time: 0.546 seconds
5/26/2013 1:40:40 PM | Total Elapsed Time: 39.624 seconds
5/26/2013 1:40:40 PM | Process finished.

I found a script online that seems close, but I haven't been able to get it to work. Can someone help? Here's the script

Code:
computer = CreateObject("WScript.Network").ComputerName

today = vbsFormat(Now, "M/d/yyyy h:mm:ss tt")

'MsgBox(today)
Set fso = CreateObject("Scripting.FileSystemObject")

Set input  = fso.OpenTextFile("c:\action.txt", 1)
Set output = fso.OpenTextFile("c:\error.txt", 2)

Do Until input.AtEndOfStream
  line = input.ReadLine
  'If Left(line, Len(today)+1) = "[" & today Then
  If Left(line, Len(today)) = today Then
    ' line timestamped with today's date
    If InStr(line, "error") > 0 Then
      ' line contains "error"
      output.WriteLine line & vbTab & input.ReadLine & vbTab & computer
    End If
  End If
Loop

input.Close
output.Close

Public Function vbsFormat(Expression, Format)
    vbsFormat = CoreFormat("{0:" & Format & "}", Expression)
End Function

' Allows more of the .NET formatting functionality to be used directly if required
Public Function CoreFormat(Format, Expression)
    CoreFormat = Expression
    On Error Resume Next
    With CreateObject("System.Text.StringBuilder")
        .AppendFormat Format, Expression
        If Err=0 Then CoreFormat = .toString
    End With
End Function
 
I think I figured it out

Code:
'==========================================================================
'
' NAME: Error Log File Check by Date.vbs
'
' AUTHOR: Gene Magerr
' EMAIL: genemagerr@hotmail.com
'
' Comment: This script checks the file \\Shcwebtrans\logs\shipconfirmemail.log for
' the word error, that have occurred on the current date. It then sends an email
' to whomever is specified, alerting them that there was an error. The errors are
' sent in the email. 
' 
' Version - 1.0 5/27/2013
'
' You have a royalty-free right to  use, modify, reproduce, and
' distribute this script file in any  way you find useful, provided that
' you agree that the creator, owner  above has no warranty, obligations,
' or liability for such use.
'
'==========================================================================
'Option Explicit
'On Error Resume Next

'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objFSO, strInput, strOutput, strLine, objEmail

Set objFSO = CreateObject("Scripting.FilesystemObject")

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
StartTime = Now 'For seeing how long the script takes to run

varTodaysDate = vbsFormat(Now, "M/dd/yyyy")

Set strInput   = objFSO.OpenTextFile("\\shcfile01\Home\gmagerr\shipconfirmemail.log", 1)
Set strOutput = objFSO.OpenTextFile("c:\error.txt", 2)

Do Until strInput.AtEndOfStream
  strLine = strInput.ReadLine
  If Left(strLine,  Len(varTodaysDate)) = varTodaysDate Then  'line timestamped with today's date
    If InStr(1,strLine, "error",1) > 0 Then 'Checking for the word error, writing to the error log. 
      strOutput.WriteLine strLine & vbCrLf
    End If
  End If
Loop

strInput.Close
strOutput.Close

Set strForEmail = objFSO.OpenTextFile("c:\error.txt",1)
strEmail = strForEmail.ReadAll
strForEmail.Close

EndTime = Now

SendEmail

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
Sub SendEmail

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "gmagerr@shieldhealthcare.com"
objEmail.To = "gmagerr@shieldhealthcare.com"
objEmail.Subject = "EDI  Error Checker"

objEmail.TextBody = objEmail.TextBody & ("This is a test of the EDI Error Checker") & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Errors Today:" & vbCrLf & vbCrLf & strEmail
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Started: = " & StartTime
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Finished: = " & EndTime & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Total Run Time: " & DateDiff("n", StartTime, EndTime) & " Minutes" 

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] = "exchange" 
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

End Sub

Public Function vbsFormat(Expression, Format)
    vbsFormat = CoreFormat("{0:" & Format & "}",  Expression)
End Function

' Allows more of the .NET formatting  functionality to be used directly if required
Public Function CoreFormat(Format, Expression)
    CoreFormat = Expression
    On Error Resume Next
    With CreateObject("System.Text.StringBuilder")
        .AppendFormat Format, Expression
        If Err=0 Then  CoreFormat = .toString
    End With
End Function
 
Yes, it's worked out great. I posted this, but could not modify my post. I changed my script and put a link to your function for cred. Thanks
 
Bummer,

I thought this script was working, but it's not. I clearly had some errors in the errorlog yesterday that didn't get picked up. I want to look through this log for the word error, starting at yesterday's date and checking through todays. Here's the code, if I leave the variable varTodaysDate in the strInput Loop I'll see an error for today, if I change that to varYesterday, I get nothing returned. Here's the code

Code:
'Option Explicit
'On Error Resume Next

'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objFSO, strInput, strOutput, strLine, objEmail, strEmail, strForEmail
Dim strPath, strComputer, strFolder, StartTime, varTodaysDate, varYesterday

Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = WScript.CreateObject( "WScript.Network" )

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
strPath = Wscript.ScriptFullName 'Will show which directory from which the script ran
strComputer = objNetwork.ComputerName

Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

StartTime = Now 'To calculate script run time

varTodaysDate = vbsFormat(Now, "M/dd/yyyy")
varYesterday = DateAdd("d", -1, varTodaysDate)
'MsgBox(varYesterday)

'Actual log files located here on the server server A:\server\logs
'Set strInput = objFSO.OpenTextFile("A:\server\logs\shipconfirmemail.log", 1)
'Set strOutput = objFSO.OpenTextFile("errorlog.txt", 2)
Set strInput = objFSO.OpenTextFile("\\file01\Home\gmagerr\shipconfirmemail.log", 1)
Set strOutput = objFSO.OpenTextFile("\\file01\Home\gmagerr\errorlog.txt", 2)

Do Until strInput.AtEndOfStream
  strLine = strInput.ReadLine
  If Left(strLine,  Len(varTodaysDate)) = varTodaysDate Then  'line timestamped with today's date
    If InStr(1,strLine, "error",1) > 0 Then 'Checking for the word error, writing to the error log. 
      strOutput.WriteLine strLine & vbCrLf 
    End If
  End If
Loop

strInput.Close
strOutput.Close 

Set strForEmail = objFSO.OpenTextFile("\\file01\Home\gmagerr\errorlog.txt", 1)

Do While Not strForEmail.AtEndOfStream
strEmail = strForEmail.ReadAll
Loop

strForEmail.Close

If strEmail = "" Then
strEmail = "No errors today"
Call SendEmailIT
Else
strEmail = strEmail
Call SendEmailDev
End If 

EndTime = Now

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
Sub SendEmailIT

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "networkautomation@comp.com"
objEmail.To = "gmagerr@comp.com"'"itoperations@comp.com"
objEmail.Subject = "shipconfirmemail log check"

objEmail.TextBody = objEmail.TextBody & ("Checking the server shipconfirmemail.log for errors") & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Errors Today:" & vbCrLf & vbCrLf & strEmail & vbCrLf 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Location:" & vbCrLf & "ComputerName: " & strComputer 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Path: " & strPath & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Started: = " & StartTime
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Finished: = " & EndTime & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Total Run Time: " & DateDiff("n", StartTime, EndTime) & " Minutes" 

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] = "exchange" 
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

End Sub

Sub SendEmailDev

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "networkautomation@comp.com"
objEmail.To = "gmagerr@comp.com"'"itdevelopers@comp.com"
objEmail.CC = "gmagerr@comp.com"'"itoperations@comp.com"
objEmail.Subject = "shipconfirmemail log check"

objEmail.TextBody = objEmail.TextBody & ("Checking the server shipconfirmemail.log for errors") & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Errors Today:" & vbCrLf & vbCrLf & strEmail & vbCrLf 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Location:" & vbCrLf & "ComputerName: " & strComputer 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Path: " & strPath & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Started: = " & StartTime
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Finished: = " & EndTime & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Total Run Time: " & DateDiff("n", StartTime, EndTime) & " Minutes" 

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] = "exchange" 
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

End Sub

Public Function vbsFormat(Expression, Format)
    vbsFormat = CoreFormat("{0:" & Format & "}",  Expression)
End Function

' Allows more of the .NET formatting  functionality to be used directly if required
Public Function CoreFormat(Format, Expression)
    CoreFormat = Expression
    On Error Resume Next
    With CreateObject("System.Text.StringBuilder")
        .AppendFormat Format, Expression
        If Err=0 Then  CoreFormat = .toString
    End With
End Function
 
varYesterday is derived from DateAdd, which returns a date value, and you are later doing a string comparison on that. If you convert the value to a string it should work.

varYesterday = [highlight #FCE94F]CStr([/highlight]DateAdd("d", -1, varTodaysDate)[highlight #FCE94F])[/highlight]
 
guitarzan,

Nice!
That worked, but it's only checking yesterdays date. How can I make the search do both yesterday and todays date?

Thanks
 
I think I got it now

Code:
'Option Explicit
'On Error Resume Next

'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objFSO, strInput, strOutput, strLine, objEmail, strEmail, strForEmail
Dim strPath, strComputer, strFolder, StartTime, varTodaysDate, varYesterday

Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = WScript.CreateObject( "WScript.Network" )

'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8

'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
strPath = Wscript.ScriptFullName 'Will show which directory from which the script ran
strComputer = objNetwork.ComputerName

Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

StartTime = Now 'To calculate script run time

varTodaysDate = vbsFormat(Now, "M/dd/yyyy")
varYesterday = CStr(DateAdd("d", -1, varTodaysDate))
'MsgBox(varYesterday)

'Actual log files located here on the server A:\server\logs
'Set strInput = objFSO.OpenTextFile("A:\server\logs\shipconfirmemail.log", 1)
'Set strOutput = objFSO.OpenTextFile("errorlog.txt", 2)
Set strInput = objFSO.OpenTextFile("\\file01\Home\gmagerr\shipconfirmemail.log", 1)
Set strOutput = objFSO.OpenTextFile("\\file01\Home\gmagerr\errorlog.txt", 2)

Do Until strInput.AtEndOfStream
  strLine = strInput.ReadLine
  If Left(strLine,  Len(varYesterday)) = varYesterday Or Left(strLine,  Len(varTodaysDate)) = varTodaysDate Then  'line timestamped with today's date
    If InStr(1,strLine, "error",1) > 0 Then 'Checking for the word error, writing to the error log. 
      strOutput.WriteLine strLine & vbCrLf 
    End If
  End If
Loop

strInput.Close
strOutput.Close 

Set strForEmail = objFSO.OpenTextFile("\\file01\Home\gmagerr\errorlog.txt", 1)

Do While Not strForEmail.AtEndOfStream
strEmail = strForEmail.ReadAll
Loop

strForEmail.Close

If strEmail = "" Then
strEmail = "No errors today"
Call SendEmailIT
Else
strEmail = strEmail
Call SendEmailDev
End If 

EndTime = Now

'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
Sub SendEmailIT

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "networkautomation@comp.com"
objEmail.To = "gmagerr@comp.com"'"itoperations@comp.com"
objEmail.Subject = "shipconfirmemail log check"

objEmail.TextBody = objEmail.TextBody & ("Checking the server shipconfirmemail.log for errors") & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Errors Today:" & vbCrLf & vbCrLf & strEmail & vbCrLf 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Location:" & vbCrLf & "ComputerName: " & strComputer 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Path: " & strPath & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Started: = " & StartTime
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Finished: = " & EndTime & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Total Run Time: " & DateDiff("n", StartTime, EndTime) & " Minutes" 

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] = "exchange" 
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

End Sub

Sub SendEmailDev

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "networkautomation@comp.com"
objEmail.To = "gmagerr@comp.com"'"itdevelopers@comp.com"
objEmail.CC = "gmagerr@comp.com"'"itoperations@comp.com"
objEmail.Subject = "shipconfirmemail log check"

objEmail.TextBody = objEmail.TextBody & ("Checking the server shipconfirmemail.log for errors") & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Errors Today:" & vbCrLf & vbCrLf & strEmail & vbCrLf 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Location:" & vbCrLf & "ComputerName: " & strComputer 
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Path: " & strPath & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Started: = " & StartTime
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Script Finished: = " & EndTime & vbCrLf
objEmail.TextBody = objEmail.TextBody & vbCrLf & "Total Run Time: " & DateDiff("n", StartTime, EndTime) & " Minutes" 

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] = "exchange" 
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send

End Sub

Public Function vbsFormat(Expression, Format)
    vbsFormat = CoreFormat("{0:" & Format & "}",  Expression)
End Function

' Allows more of the .NET formatting  functionality to be used directly if required
Public Function CoreFormat(Format, Expression)
    CoreFormat = Expression
    On Error Resume Next
    With CreateObject("System.Text.StringBuilder")
        .AppendFormat Format, Expression
        If Err=0 Then  CoreFormat = .toString
    End With
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top