Here is the PowerShell Script so far:
It monitors the folder specified beautifully but generates two entries on file changes.
Is there a way to consolidate or just return one event?
Get-EventSubscriber | Unregister-Event
$fsw = New-Object System.IO.FileSystemWatcher
$fsw.Path = 'c:\ftptest'
$fsw.Filter = '*.*'
$fsw.EnableRaisingEvents = $true
$fsw.IncludeSubdirectories = $true
$fsw.NotifyFilter = [IO.NotifyFilters]::LastWrite
$null = Register-ObjectEvent -InputObject $fsw -EventName Changed -SourceIdentifier MonitorFiles
while ($true)
{
$evt = Wait-Event -SourceIdentifier MonitorFiles
Write-Host "Changed: $($evt.SourceEventArgs.FullPath)"
$evt | Remove-Event
}
When I edit a file and save it, it outputs the file changed twice. Changed: c:\ftptest\Test1.txt Changed: c:\ftptest\Test1.txt
Ok I have changed the code to this which seems to help - a kind of flip-flop if you will...
Get-EventSubscriber | Unregister-Event
$fsw = New-Object System.IO.FileSystemWatcher
$fsw.Path = 'c:\ftptest'
$fsw.Filter = '*.*'
$fsw.EnableRaisingEvents = $true
$fsw.IncludeSubdirectories = $true
$fsw.NotifyFilter = [IO.NotifyFilters]::LastWrite
$null = Register-ObjectEvent -InputObject $fsw -EventName Changed -SourceIdentifier MonitorFiles
$Buffer = ""
while ($true)
{
$evt = Wait-Event -SourceIdentifier MonitorFiles
if ($Buffer -eq "Changed: $($evt.SourceEventArgs.FullPath)") {
$Buffer = ""
} Else {
$Buffer = "Changed: $($evt.SourceEventArgs.FullPath)"
}
Write-Host $Buffer.ToString()
$evt | Remove-Event
}