首页 > 解决方案 > FileSystemWatcher 未检测到 mp4 文件

问题描述

下面的脚本应该监视指定文件夹中的“.mp4”文件,并在创建文件并停止运行时记录一个事件。但是在运行脚本时,它不会在创建时检测 mp4 文件,而只会将作业显示为未启动(即使文件夹中存在预先存在的 .mp4 文件,它仍将作业显示为未启动)。对于过滤器,我尝试使用“*.mp4*”、“input.mp4”和“*input.mp4*”,但文件检测没有运气。

要让脚本检测这些 mp4 文件,还需要进行哪些额外的编辑?

Get-ChildItem 检测到创建的 input.mp4 文件 input.mp4 文件创建后的作业状态

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "D:\Script\Powershell"
$watcher.Filter = "*input.mp4*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true  
$loop = $true

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = {
    #LOG EVENT
    $path = $Event.SourceEventArgs.FullPath
    $changeType = $Event.SourceEventArgs.ChangeType
    $logline = "$(Get-Date), $changeType, $path"
    Add-Content "D:\log.txt" -Value $logline

    ###Stop whileloop  after filedetection
    $script:loop = $false
}   

### DECIDE WHICH EVENTS SHOULD BE WATCHED
while ($loop) {
    Register-ObjectEvent -InputObject $watcher "Created" -Action $action
    #Register-ObjectEvent $watcher "Changed" -Action $action
    #Register-ObjectEvent $watcher "Deleted" -Action $action
    #Register-ObjectEvent $watcher "Renamed" -Action $action
    (Start-Sleep -s 1)
}

#UNREGISTER EVENT
Unregister-Event -SubscriptionId 1 -Verbose

编辑 1
上面的脚本从未进入“动作块”,因此没有检测到文件,所以我在动作块中添加了一个新事件脚本(第 15 行)。但它仍然没有进入它并执行动作 {of 'write-output "Action Started"' 在这种情况下}。请告诉我,执行操作块需要进行哪些额外的编辑。

控制台输出

### SET FOLDER TO WATCH 
  $watcher = New-Object System.IO.FileSystemWatcher
  $watcher.Path = "D:\Script\Powershell"
  $watcher.Filter = "*input.mp4*"
  $watcher.IncludeSubdirectories = $true
  $watcher.EnableRaisingEvents = $true  
  $loop = $true


### DEFINE ACTIONS AFTER AN EVENT IS DETECTED

  $action = {New-Event  -SourceID "Ps1.FileCreated"  #<--New-event Added

    Write-output "Action Start" #<--OutputAction started to Console  

    #LOG EVENT
    $path = $Event.SourceEventArgs.FullPath
    $changeType = $Event.SourceEventArgs.ChangeType
    $logline = "$(Get-Date), $changeType, $path"
    Add-content "D:\log.txt" -value $logline



    ###Change $loop value after compression

    $Script:loop  = $false
    Write-Output "Action Stopped" #<--OutputAction stopped to Console  


} 


### DECIDE WHICH EVENTS SHOULD BE WATCHED 

  while ($loop) {

    Write-Output "Loop Entered "

    Register-ObjectEvent -InputObject $watcher "Created"  -Action $action 

    <#
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    #>
    Write-Output "Loop Exit "
    (Start-Sleep -s 1);
  }


#UNREGISTER EVENT
    Unregister-Event -SubscriptionId 1 -Verbose

标签: videopowershell-3.0filesystemwatcher

解决方案


不需要循环。

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "D:\Temp"
$watcher.Filter = "*.mp4*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true  
$action = {
    Write-Host $Event.SourceEventArgs.ChangeType $Event.SourceEventArgs.FullPath
}
Register-ObjectEvent -InputObject $watcher -EventName "Created" -Action $action

推荐阅读