VBScript to monitor multiple files timestamp

Golfgent
Golfgent used Ask the Experts™
on
I'm trying to write a script which will monitor the timestamps for multiple files.  If the variance is over the alotted time then an alert is emailed indicating that the threshold has exceeded the timestamp.  I've been able to create this for one file, however I need to monitor several files and there are two different time thresholds.  Two files updates every 30 minutes and the third updates every twelve hours.  
strComputer = "Calvin"
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_Datafile Where Name = '\\Downtime_eMAR\\eMAR_ADMINISTRATION_REPORT'")
 
For Each objFile in colFiles
    strOriginalTimestamp = objFile.LastModified
Next
 
Wscript.Echo "Monitoring eMAR_ADMINISTRATION_REPORT file: " & Now
Wscript.Echo
 
Do While True
    Wscript.Sleep 1800000
    Set colFiles = objWMIService.ExecQuery _
        ("Select * from CIM_Datafile Where Name = 'D:\\Downtime_eMAR\\eMAR_ADMINISTRATION_REPORT'")
 
    For Each objFile in colFiles
        strLatestTimestamp = objFile.LastModified
    Next 
 
    If strLatestTimestamp <> strOriginalTimestamp Then
        strOriginalTimestamp = strLatestTimeStamp
    Else
        Set objEmail = CreateObject("CDO.Message")
 
objEmail.From = "emarbackup@xyz.local"
objEmail.To = "jc@xyz.com, networkadmins@xyz.com,"
objEmail.Subject = "Testing....testing...testing...eMAR_ADMINISTRATION_REPORT timestamp exceeds threshold" 
objEmail.Textbody = "The eMAR_ADMINISTRATION_REPORT timestamp exceeds the alloted time threshold."
 
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        "mailbox" 
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
 
objEmail.Send
 
    End If
Loop

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ive done something similar in the past.  The way I did it was to have a counter that increments at the end of each loop.  Then, at the beginning of each part, check it to see if it is time to run.  For your example, I would put a iCount variable at the very end that adds 1

iCount=iCount +1

Then since the sleep time is 30 minutes, the first two files will be checked every loop, and the last one will be checked every 24 count like so...

If iCount mod 24 = 0 then
    your code here
End if

Hope this helps.

Author

Commented:
TakedaT,
Thanks for the input, but I'm a newbie at writing vbscript and have spent a couple of hours researching using iCount and I'm still not sure how to use it in this instance.  So, can you please assist?

Do I go ahead and add the other two files after the following?

Do While True
    Wscript.Sleep 1800000
    Set colFiles = objWMIService.ExecQuery _
        ("Select * from CIM_Datafile Where Name = 'D:\\Downtime_eMAR\\eMAR_ADMINISTRATION_REPORT'")

And more specifically would I just put a comma seperator after the first file?

Second, Would I then create a separate Do while true for each file?

Thanks for your input?
I think this is what you need.  Put the other 2 file paths in the 3rd and 4th lines.  It should send a different email for each file.  If you would like to combine the status of all 3 files into 1 email, it can be done with mods.  I cant test it without being in the environment, so let me know if you run into problems.
strComputer = "Calvin"
strFirstFile = "D:\\Downtime_eMAR\\eMAR_ADMINISTRATION_REPORT"
strSecondFile = ""
strThirdFile = ""
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
iCount = 0
 
Do While True  
    'FIRST FILE - EVERY 30 MINUTES----------------------------------
        Set colFiles = objWMIService.ExecQuery _
        ("Select * from CIM_Datafile Where Name = '"&strFirstFile&"'")
    For Each objFile in colFiles
        str1stTimestamp = objFile.LastModified
    Next 
    If str1stTimestamp <> str1stOrigTimestamp Then
        str1stOrigTimestamp = str1stTimeStamp
    Else
		SendEmail strFirstFile&" timestamp exceeds threshold.",strFirstFile&" timestamp exceeds threshold."
	End If      
    'SECOND FILE - EVERY 30 MINUTES----------------------------------
    Set colFiles = objWMIService.ExecQuery _
        ("Select * from CIM_Datafile Where Name = '"&strSecondFile&"'")
    For Each objFile in colFiles
        str2ndTimestamp = objFile.LastModified
    Next 
    If str2ndTimestamp <> str2ndOrigTimestamp Then
        str2ndOrigTimestamp = str2ndTimestamp
    Else
		SendEmail strSecondFile&" timestamp exceeds threshold.",strSecondFile&" timestamp exceeds threshold."
	End If
    'THIRD FILE ONLY EVERY 24 LOOPS - EVERY 12 HOURS----------------------------------
	If iCount mod 24 = 0 then
	    Set colFiles = objWMIService.ExecQuery _
	        ("Select * from CIM_Datafile Where Name = '"&strThirdFile&"'")
	    For Each objFile in colFiles
	        str3rdTimestamp = objFile.LastModified
	    Next 
	    If str3rdTimestamp <> str3rdOrigTimestamp Then
	        str3rdOrigTimestamp = str3rdTimestamp
	    Else
			SendEmail strThirdFile&" timestamp exceeds threshold.",strThirdFile&" timestamp exceeds threshold."
		End If 
	End If
    iCount = iCount + 1
    Wscript.Sleep 1800000
Loop
 
Function SendEmail(strSubject,strBody)
		Set objEmail = CreateObject("CDO.Message")
 		objEmail.From = "emarbackup@xyz.local"
		objEmail.To = "jc@xyz.com, networkadmins@xyz.com,"
		objEmail.Subject = strSubject
		objEmail.Textbody = strBody
		objEmail.Configuration.Fields.Item _
		    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
		objEmail.Configuration.Fields.Item _
		    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
		        "mailbox" 
		objEmail.Configuration.Fields.Item _
		    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
		objEmail.Configuration.Fields.Update
		objEmail.Send
End Function

Open in new window

BTW, sorry for confusing you with the iCount.  It is simply a variable for keeping count of loops.  It could have been named anything you wish.

Author

Commented:
Thanks TakedaT for the excellent help and prompt response.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial