【问题标题】:Powershell : FileSystemWatcher for filename changePowershell:用于文件名更改的 FileSystemWatcher
【发布时间】: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


【解决方案1】:

仅将 $watcher 变量中的 NotifyFilter 属性设置为 FileName。

$watcher.NotifyFilter = 'FileName'

现在该操作只会在文件名更改时触发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    相关资源
    最近更新 更多