• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

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.
0
murryc
Asked:
murryc
  • 5
  • 4
1 Solution
 
ltlbearand3Commented:
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

0
 
murrycAuthor Commented:
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.
0
 
ltlbearand3Commented:
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
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
murrycAuthor Commented:
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

0
 
ltlbearand3Commented:
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
0
 
murrycAuthor Commented:
Just got back from vacation.  Will be finalizing this tomorrow.  Updates to follow.
0
 
murrycAuthor Commented:
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.
0
 
ltlbearand3Commented:
murryc,

Sorry.  Somehow I missed seeing the email that you had replied.  I did not create the original code to create the two DSM files - only to modify them.  I have adjusted the code and it will create them if they do not exist.  If this code works just put a tick mark "'" in front of each line like:
'msgbox
and it will not display the box anymore.

-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:\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
		Else
			Set objLog = objFSO.CreateTextFile(strLog2)
		End IF
		objLog.WriteLine "Whatever you want to write to the Log"
		objLog.close
	Else
		' Write to Log
		If objFSO.FileExists(strLog3) Then
			Set objLog = objFSO.OpenTextFile(strLog3, 8) ' 8 = for appending
		Else
			Set objLog = objFSO.CreateTextFile(strLog3)
		End IF
		objLog.WriteLine "Whatever you want to write to the Log"
		objLog.close
		
		' 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

0
 
murrycAuthor Commented:
Great Work!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now