There’s a scenario where there’s an active folder where files are coming in and out, so the key objective of the program is to ensure that the folder always stay empty. Any files that stay more that 30 minutes is consider an alert and it needs to be taken care of and the alert will be sent through email.
So below are the code and it is triggered every 15 minutes using the Task Scheduler :
Note: The Outlook application has to be running, otherwise it will go to the outbox folder and the mail will be sent after the outlook application is running
age_threshold = 30 'in minutes
folderPath = "path"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.getFolder(folderPath )
file_found = 0
Set shell = wscript.CreateObject("Shell.Application")
For Each fl in fldr.Files
age = DateDiff("n", fl .DateCreated, Now)
If age > age_threshold Then
file_found = file_found+1
if file_found > 0 then
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.Display 'To display message
objMail.To = "firstname.lastname@example.org"
objMail.Subject = "Alert: Folder XYZ"
objMail.Body = "Please check the XYZ folder - "&folderPath
Set objMail = Nothing
Set objOutlook = Nothing