问题
I would like to get an Notification when a certain file where changed. It works fine but I can't set the NotifyFilter. "is not recognized as the name of a cmdlet..." The main problem is: That my script goes twice in my function. A change is identified and then I get two times "changed"
function fileChanged ($text) {
Write-Host $text
}
$watcher = New-object System.IO.FileSystemWatcher "C:\Users\test\Desktop\"
$watcher.EnableRaisingEvents = $true
$watcher.Filter="*.txt"
$watcher.NotifyFilter = (System.IO.NotifyFilters.LastWrite)
$changed = Register-ObjectEvent $watcher "Changed" -Action {
$txt = "changed"
filechanged($txt)
}
while($true) {
echo "wartet"
$null = $watcher.WaitForChanged("Changed")
start-sleep -s 2
}
回答1:
change this in your code for declaring notifyfilter:
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
But this not solve the issue, read this:
From the "Troubleshooting FileSystemWatcher Components" section of the VS.NET documentation...
Multiple Created Events Generated for a Single Action You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.
Note: Notepad may also cause other interesting event generations. For example, if you use the ChangeEventFilter to specify that you want to watch only for attribute changes, and then you write to a file in the directory you are watching using Notepad, you will raise an event . This is because Notepad updates the Archived attribute for the file during this operation.
回答2:
WaitForChanged is explicitly using the FileSystemWatcher to synchronously wait for changes. EnableRaisingEvents is performing an asyncrhonous wait: the event handlers will be called when something changes.
You are simultaneously looking for changes synchronously and asynchronously. Chose one and use that, using both is going to confuse everything.
回答3:
Found the following was the most simple:
Credit to Derek Newton at: http://dereknewton.com/2011/05/monitoring-file-system-changes-with-powershell/
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $searchPath
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$changed = Register-ObjectEvent $watcher "Changed" -Action {
write-host "Changed: $($eventArgs.FullPath)"
}
来源:https://stackoverflow.com/questions/7791979/lastwrite-filesystemwatcher-powershell-notification