我尝试使用此代码来监控文件服务器上的文件名更改。但我只想在扩展名发生变化时触发事件。
任何建议?
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "E:\"
# $watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$action = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$Name = $Event.SourceEventArgs.Name
$logline = "$(Get-Date), $changeType, $path, $name"
Add-content "E:\log.txt" -value $logline
}
Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}
答案 0 :(得分:0)
将$ watcher变量中的NotifyFilter属性设置为仅FileName。
$watcher.NotifyFilter = 'FileName'
现在该操作只会触发文件名更改。