Watch Folder For A Missing File

I am already using a script to watch a folder for when certain files show up and send an email. However, I had a user ask me, can I create something to monitor for when a file doesn’t show up?

We have a folder where a certain file with the name Dump-362-Month-Day-Year.xml is supposed to show up every night at 11:15. This file is then processed by another process at 2AM. A group would like to be notified if the file is missing.

I have tried figuring out how to do this, and am not sure where to really start to look for a missing file.

Is this possible and can someone give me some pointers on how to get started? I am thinking that somehow you look for a file with the days date (and maybe time stamp, since it’s always 11:15), and set a flag so that if the flag is false then it sends an email.

This is the script I am using to watch a folder when a file appears.
$folder = ‘c:\crash’ # Enter the root path you want to monitor.
$filter = ‘*.crs’ # You can enter a wildcard filter here.

In the following line, you can change 'IncludeSubdirectories to $true if required.

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]‘FileName, LastWrite’}

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated

Send-MailMessage -To -From -Subject “File Added to Crash Directory” -Body “The file ‘$name’ was $changeType at $timeStamp” -SmtpServer


There isn’t an event you can tap for a file’s non-creation so the approach you outlined is the one I’d have used. You can either run a scheduled task at say midnight or 1am to test is the file is there and send notification. The other way would be to amend the 2am process so that it tests for the presence of the file and ends gracefully if it’s not present - including sending notification

Alright, thanks.

How do I go about setting the flag? I think I need to create a new variable, and then if the file is there, set it to true, and if there is no file set it to false. I don’t know how to take the File System Watcher object and determine if a file exists. Thoughts on this?

You won’t be using the FileSystemWatcher at all. A simple Test-Path and/or Get-Item command will do.

You are certainly correct. Test-Path worked great, thanks! Now to just figure out why the script won’t run from Operations Manager, but that’s a battle fought elsewhere. Thanks again.