【发布时间】:2017-04-05 12:33:09
【问题描述】:
我尝试使用此代码来监控文件服务器上的文件名更改。但我只想在扩展发生变化时触发事件。
有什么建议吗?
### 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}
【问题讨论】:
-
将来我建议将 powershell 标签添加到任何与 powershell 有关的问题中。这里有许多知识渊博的 powershell 用户,这将帮助他们更快地为您提供帮助!
标签: powershell filenames filesystemwatcher file-rename