Link to home
Start Free TrialLog in
Avatar of Golfgent
Golfgent

asked on

VBScript to monitor multiple files timestamp

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

Avatar of TakedaT
TakedaT
Flag of United States of America image

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.
Avatar of Golfgent
Golfgent

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of TakedaT
TakedaT
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
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.
Thanks TakedaT for the excellent help and prompt response.