我可以成功地使用 System.IO.FileSystemWatcher 来监视文件夹和子目录中创建的文件,但是当我尝试使用 Start Process 运行批处理文件时,它只运行一次,但之后再也不会触发。我修改了这里。
这是我的 powershell 脚本
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\lectures"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$action = { $path = $Event.SourceEventArgs.FullPath
Write-Host "Event Fired"
$ext = [io.path]::GetExtension($path)
if($ext -eq ".wma"){
$file = [io.path]::GetFileNameWithoutExtension($path)
$folder = Split-Path(Split-Path $path -Parent) -Leaf
$date = Get-Date -UFormat "%Y-%m-%d"
$newName = $folder + "_" + $date + $ext
$newPath = Rename-Item -path $path -newname $newName -PassThru
Start-Process "C:\lectures\deploy.bat" $newPath
}
}
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {
Write-Host "Still Alive"
sleep 5
}
这是我正在运行的批处理文件
@echo off
CD %~dp1
ffmpeg -i %~nx1 %~n1.mp3 -loglevel panic
del %1
REM Winscp.com /ini=null /script=uploadScript.txt /parameter %~n1.mp3
powershell 脚本在运行该进程后继续运行,并且该进程似乎正常退出。如何让它继续处理事件?