Link to home
Start Free TrialLog in
Avatar of IT CAMPER
IT CAMPERFlag for United States of America

asked on

Batch file to check log file every 30 minutes

I need to check a log file every 30 minutes (which I can use the Windows scheduler) to see if it has been updated within the last 30 minutes.  The logfile is GeodeScott.YYYYMMDD.log and changes every day.  The modified date will reflect the changes written to it, so the batch file needs to check today's date, find the log file that matches the filename structure above, check to see if the modified date is within the last 30 minutes and then write to a log file.  If the log file has failed to update within the last 30 minutes, then I need to write that to a log file and use blat to email it.
Avatar of ltlbearand3
ltlbearand3
Flag of United States of America image

Below is a vb script that should do what you need.  Just paste into a text editor, update the string values as needed and save as a vb script.  As I don't know all your parameters, you need to update the file paths and names.  You need to update the text to be written to the log files.  You will need to set the command line for blat.

-Bear
Option Explicit

Dim objFSO, objFile, objLog
Dim strLog1, strLog2, strLog3
Dim strDate, strModify 
Dim objShell

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell") 

' ----
' Build Data String
' ----
' Year
strDate = Year(date())
' Month
If Len(Month(date())) = 2 Then
	strDate = strDate & month(date())
Else
	strDate = strDate & ("0" & month(date()))
End If
' Day
If Len(Day(date())) = 2 Then
	strDate = strDate & Day(date())
Else
	strDate = strDate & ("0" & Day(date()))
End If


' ----
' Log File Locations - Update Values
' ----
strLog1 = "C:\GeodeScott." & strDate & ".log"
strLog2 = "C:\LogFile.log"
strLog3 = "C:\LogErrFile.log"


' Make sure file exits
If objFSO.FileExists(strLog1) Then
	' Grab last modified date
	Set objFile = objFSO.GetFile(strLog1)
	
	' Check if it has been over 30 minutes
	If DateDiff("N", objFile.DateLastModified, now()) > 30 Then
		' Write to Log
		If objFSO.FileExists(strLog2) Then
			Set objLog = objFSO.OpenTextFile(strLog2, 8) ' 8 = for appending
			objLog.WriteLine "Whatever you want to write to the Log"
			objLog.close
		End IF
	Else
		' Write to Log
		If objFSO.FileExists(strLog3) Then
			Set objLog = objFSO.OpenTextFile(strLog3, 8) ' 8 = for appending
			objLog.WriteLine "Whatever you want to write to the Log"
			objLog.close
		End IF
		
		' Send Email with Blat
		'objShell.Run "Blat - put all of your options and commands for blat here"
		
	End If
End If

Set objLog = Nothing
Set objFSO = Nothing
Set objShell = Nothing

Open in new window

Avatar of IT CAMPER

ASKER

We need some debugging I guess.  When I execute the file nothing happens.  The log files do not get created but I also do not receive any errors.
I did run the script locally and it seemed fine.  I am guessing the problem is in the setting of the vaules strLog1, strLog2, and strLog3.  Make sure all of those are correct.  A quick and dirty way to debug is to use message boxes.   I suggest the following:

The line after:
strLog3 = "C:\whateveryouputhere.log"

Open in new window

Add:
Msgbox strLog1

Open in new window

This will give you a visual of what log file it is checking the date on.

After:
Set objFile = objFSO.GetFile(strLog1)

Open in new window

Add:
msgbox "File Date = " & objFile.DateLastModified & " : Current DateTime=" & now()

Open in new window

This will give you a visual of the date and time stamp comparison.  It will not do anything if these are withing 30 minutes of each other.

After:
If DateDiff("N", objFile.DateLastModified, now()) > 30 Then

Open in new window

Add:
msgbox "Date Difference is over 30 minutes" 

Open in new window


After:
If objFSO.FileExists(strLog2) Then

Open in new window

Add:
msgbox "Updating Log File"

Open in new window


-Bear
Okay, here is what I am getting.  I have inserted your message box statements but I only get the first one.  Here is my exact code with the variables I am using.
Option Explicit

Dim objFSO, objFile, objLog
Dim strLog1, strLog2, strLog3
Dim strDate, strModify 
Dim objShell

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell") 

' ----
' Build Data String
' ----
' Year
strDate = Year(date())
' Month
If Len(Month(date())) = 2 Then
	strDate = strDate & month(date())
Else
	strDate = strDate & ("0" & month(date()))
End If
' Day
If Len(Day(date())) = 2 Then
	strDate = strDate & Day(date())
Else
	strDate = strDate & ("0" & Day(date()))
End If


' ----
' Log File Locations - Update Values
' ----
strLog1 = "C:\Surf\GeodeScott." & strDate & ".log"
strLog2 = "C:\Surf\DSM\LogFile.log"
strLog3 = "C:\Surf\DSM\LogErrFile.log"
		Msgbox strLog1 


' Make sure file exits
If objFSO.FileExists(strLog1) Then
	' Grab last modified date
	Set objFile = objFSO.GetFile(strLog1)
		Msgbox "File Date = " & objFile.DateLastModified & " : Current DateTime=" & now()
	
	' Check if it has been over 30 minutes
	If DateDiff("N", objFile.DateLastModified, now()) > 30 Then
		msgbox "Date Difference is over 30 minutes" 
		' Write to Log
		If objFSO.FileExists(strLog2) Then
			msgbox "Updating Log File"
			Set objLog = objFSO.OpenTextFile(strLog2, 8) ' 8 = for appending
			objLog.WriteLine "Whatever you want to write to the Log"
			objLog.close
		End IF
	Else
		' Write to Log
		If objFSO.FileExists(strLog3) Then
			Set objLog = objFSO.OpenTextFile(strLog3, 8) ' 8 = for appending
			objLog.WriteLine "Whatever you want to write to the Log"
			objLog.close
		End IF
		
		' Send Email with Blat
		'objShell.Run "Blat - put all of your options and commands for blat here"
		
	End If
End If

Set objLog = Nothing
Set objFSO = Nothing
Set objShell = Nothing

Open in new window

murryc,

The first message box, just gives you a visual of what file it is looking for.  When it pops up navigate and make sure that that exact file path and name does exist.  The code then checks for the existence of this file.  If it finds the file it will display a second message box with the Last Modified date of the file and the current time stamp.  That way for debugging purposes, you can verify the file is over 30 minutes old.  The fact that you never see the second message box leads me to believe that file name stored in strLog1 is somehow incorrect since the script never finds that file.  Check what the difference is and we can adjust the code.  You can post what is displayed in the message box and what is the actual file name at the time the code was run.

-Beary
Just got back from vacation.  Will be finalizing this tomorrow.  Updates to follow.
Okay, the message windows are confirming the filename, the date on the file versus the system date and also reports when the file date is past the 30 minute window.  The only thing left is that the logs are not being created in the DSM folder.  The folder is empty.
ASKER CERTIFIED SOLUTION
Avatar of ltlbearand3
ltlbearand3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great Work!