VB Script - File Monitoring

Rupert Eghardt
Rupert Eghardt used Ask the Experts™
on
Hi Guys,

I have a working VB Script - sending an e-mail.  (Please see script below)
I would like to modify the script to include a conditional trigger.

The script will be called every 5 minutes from Windows Task Scheduler, but should only trigger an e-mail, if a "file being monitored" is older than 15 minutes.

For example:  We have a reporting tool, writing a file to:  C:\OUTPUT\Report.PDF

We need an e-mail notification, if the time-stamp of the file (Report.PDF) is older than 15 minutes (Thus not replaced or updated)

My Current Working VB Script:

on error resume next
 
Const schema   = "http://schemas.microsoft.com/cdo/configuration/"
Const cdoBasic = 1
Const cdoSendUsingPort = 2
Dim oMsg, oConf
 
' E-mail properties
Set oMsg      = CreateObject("CDO.Message")
oMsg.From     = "user@domain.com"  ' or "Sender Name <from@gmail.com>"
oMsg.To       = "user@domain.com"    ' or "Recipient Name <to@gmail.com>"
oMsg.Subject  = "Messaging Script"
oMsg.TextBody = "Messaging Script"
 
' GMail SMTP server configuration and authentication info
Set oConf = oMsg.Configuration
oConf.Fields(schema & "smtpserver")       = "mail.domain.com" 'server address
oConf.Fields(schema & "smtpserverport")   = 587              'port number
oConf.Fields(schema & "sendusing")        = cdoSendUsingPort
oConf.Fields(schema & "smtpauthenticate") = cdoBasic         'authentication type
oConf.Fields(schema & "smtpusessl")       = "False"             'use SSL encryption
oConf.Fields(schema & "sendusername")     = "user@domain.com" 'sender username
oConf.Fields(schema & "sendpassword")     = "Password"      'sender password
oConf.Fields.Update()
 
' send message
oMsg.Send()
 
' Return status message
If Err Then
    resultMessage = "ERROR " & Err.Number & ": " & Err.Description
    Err.Clear()
Else
    resultMessage = "Message sent ok"
End If
 
'Wscript.echo(resultMessage)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT / Software Engineering Consultant
Top Expert 2016
Commented:
On the road so can't test this, but it should hopefully give you enough of the idea to get what you want.  Take a look and see if it makes sense.

on error resume next
 
Const schema   = "http://schemas.microsoft.com/cdo/configuration/"
Const cdoBasic = 1
Const cdoSendUsingPort = 2
Const CheckFile = "C:\OUTPUT\Report.PDF"
Const AgeInMins = 15

Dim oMsg, oConf
Dim objFSO, strFile, objFile
 
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
strFile = objFSO.GetAbsolutePathname(CheckFile)
If Not objFSO.FileExists(strFile) Then
    ' ***** WHAT DO YOU WANT TO DO IF FILE DOESN'T EXIST? *****
    Wscript.Quit
Else
    Set objFile = objFSO.GetFile(strFile)
    If DateDiff("n", objFile.DateLastModified, Now) <= AgeInMins Then
        Wscript.Quit
    End If
End If 
 
' E-mail properties
Set oMsg      = CreateObject("CDO.Message")
oMsg.From     = "user@domain.com"  ' or "Sender Name <from@gmail.com>"
oMsg.To       = "user@domain.com"    ' or "Recipient Name <to@gmail.com>"
oMsg.Subject  = "Messaging Script"
oMsg.TextBody = "Messaging Script"
 
' GMail SMTP server configuration and authentication info
Set oConf = oMsg.Configuration
oConf.Fields(schema & "smtpserver")       = "mail.domain.com" 'server address
oConf.Fields(schema & "smtpserverport")   = 587              'port number
oConf.Fields(schema & "sendusing")        = cdoSendUsingPort
oConf.Fields(schema & "smtpauthenticate") = cdoBasic         'authentication type
oConf.Fields(schema & "smtpusessl")       = "False"             'use SSL encryption
oConf.Fields(schema & "sendusername")     = "user@domain.com" 'sender username
oConf.Fields(schema & "sendpassword")     = "Password"      'sender password
oConf.Fields.Update()
 
' send message
oMsg.Send()
 
' Return status message
If Err Then
    resultMessage = "ERROR " & Err.Number & ": " & Err.Description
    Err.Clear()
Else
    resultMessage = "Message sent ok"
End If

Open in new window


»bp
Rupert EghardtProgrammer

Author

Commented:
Working well! ;-)

Thank you

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