【问题标题】:Powershell script does not wait for files in input folder, exitsPowershell脚本不等待输入文件夹中的文件,退出
【发布时间】:2014-12-23 00:45:45
【问题描述】:

在 Google 的帮助下,我为 Handbrake 自动化编写了一个 Powershell 脚本。

它的作用:

  1. 有些文件是通过 RSS 自动下载的。它们被放置在源文件夹中。
  2. Powershell 脚本执行 Handbrake,编码开始并成功结束。
  3. 如果源文件夹中没有新文件到达,则脚本退出。

问题在于最后一项。当源文件夹为空时,Powershell 脚本退出,但我希望它继续运行并在它们到达时处理更多文件,直到我杀死它。添加新文件时,它应该会自动开始编码。

代码在 PasteBin 中,它有更多的 cmets,但应该很容易推断出脚本的作用:

$inputpath = "I:\S"
$outputpath = "I:\E"

$movies = ls $inputpath

foreach($movie in $movies){
    $name = $movie.basename

    if(!(test-path -path "$outputpath\$name.mkv")){
        C:\"Program Files"\handbrake\HandBrakeCLI.exe -i "$inputpath\$movie" -o "$outputpath\$name.mkv" `
        -e x264 -b 1000 -2 -T -a 1,1 -E mp3 -B 112 --mixdown stereo -f mkv --detelecine --decomb `
        --loose-anamorphic -m -x rc-lookahead=30:ref=4:bframes=3:me=umh:subme=9:analyse=none:deblock=1:0:0:8x8dct=1
    }
}

【问题讨论】:

  • 那么您是否只是在寻找解释文件夹何时为空白的逻辑?如果您向我们展示您遇到问题的代码,可能会更好。
  • 我看到了您的代码,但不明白您的问题。如果源文件夹为空,则无需处理任何内容,因此脚本可以正常工作。你想发生什么?
  • 是的,如果源文件夹为空,它就会退出!我不希望它退出,同时从私人跟踪器下载源文件!添加新文件时,它应该会自动开始编码,但我们知道,它会在源文件夹变空后退出!我只是希望它在源文件夹变空的情况下继续工作,然后在源文件夹中收到新文件后立即开始处理。

标签: powershell handbrake


【解决方案1】:

在您的 cmets 中,您将 System.IO.FileSystemWatcherRegister-ObjectEvent 描述为用作文件监视程序。直到现在还没有玩过它,但这里有一个你正在寻找的样本。

$inputpath = "I:\S" 
$outputpath = "I:\E"

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $searchPath
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

$created = Register-ObjectEvent $watcher "Created" -Action {
    $name = $eventArgs.basename

    if(!(test-path -path "$outputpath\$name.mkv")){
        C:\"Program Files"\handbrake\HandBrakeCLI.exe -i "$($eventArgs.FullName)" -o "$outputpath\$name.mkv" `
        -e x264 -b 1000 -2 -T -a 1,1 -E mp3 -B 112 --mixdown stereo -f mkv --detelecine --decomb `
        --loose-anamorphic -m -x rc-lookahead=30:ref=4:bframes=3:me=umh:subme=9:analyse=none:deblock=1:0:0:8x8dct=1
    }    
}

基于论坛帖子here。搜索 System.IO.FileSystemWatcherRegister-ObjectEvent 可能会为您提供更多上下文。此外,您可能需要检查手刹代码,因为它在您的 pastebin 中看起来错误,我试图改进它以提高可读性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多