Programming, Solution

VBScript: Folder Monitoring

Hi,

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
End If
Next
if file_found > 0 then
sendAlert(file_found)
WScript.Quit()
else
WScript.Quit()
end if

sub sendAlert(no)
Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)
objMail.Display 'To display message
objMail.To = "xyz@xyz.com"
objMail.Subject = "Alert: Folder XYZ"
objMail.Body = "Please check the XYZ folder - "&folderPath
objMail.Send
Set objMail = Nothing
Set objOutlook = Nothing
end sub

-Farrah K.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s