0

I want to do some things when there is a change in a bunch of folders (like send an email). It is working more or less. The problem is I am getting too many notifications. I know I can hack it and keep track and ignore dups, but it seems like there should be a way to get it to work normally.

Code:

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "s:\Reporting\Report Output Files\"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

$changed = Register-ObjectEvent $watcher "Changed" -Action {
write-host "Changed: $($eventArgs.FullPath)"
}
$created = Register-ObjectEvent $watcher "Created" -Action {
write-host "Created: $($eventArgs.FullPath)"
}

while($true) {
start-sleep -s 2
}

Results when I create a file:

Created: s:\Reporting\Report Output Files\IT\Scheduled_IO.log
Changed: s:\Reporting\Report Output Files\IT\Scheduled_IO.log
Changed: s:\Reporting\Report Output Files\IT\Scheduled_IO.log
Changed: s:\Reporting\Report Output Files\IT\Scheduled_IO.log
Changed: s:\Reporting\Report Output Files\IT

I'm not sure why I am getting 3 changes (I can see why I am getting a directory level change) when really it was just a file being created. When I change a file, I am also getting dup changes.

user1612851
  • 1,144
  • 4
  • 18
  • 32

0 Answers0