video - FileSystemWatcher 未检测到 mp4 文件
问题描述
下面的脚本应该监视指定文件夹中的“.mp4”文件,并在创建文件并停止运行时记录一个事件。但是在运行脚本时,它不会在创建时检测 mp4 文件,而只会将作业显示为未启动(即使文件夹中存在预先存在的 .mp4 文件,它仍将作业显示为未启动)。对于过滤器,我尝试使用“*.mp4*”、“input.mp4”和“*input.mp4*”,但文件检测没有运气。
要让脚本检测这些 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
解决方案
不需要循环。
$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
推荐阅读
- javascript - javascript框获取div中的位置x
- angular - 如何根据条件更改容器组件,避免重复其内容
- dart - 使用 num 的 int 和 double 上的抽象被新的 Dart 版本破坏
- swift - UIBarButtonItem 以编程方式更改颜色
- ios - 使用 CLLocationCoordinate2D 的矩形
- linux - 从 vagrant box 从浏览器访问文件
- python - ModuleNotFoundError:导入 visdom 时没有名为“visdom”的模块
- javascript - 如何使用钩子在反应中切换单选按钮的选中值?
- python - 我应该如何使用 OpenCV 去除这张图片中的噪点?
- python - python 3中的问题:创建二维列表时出现“IndexError:列表索引超出范围”