【问题标题】:Run start-process after foreach loop在 foreach 循环后运行 start-process
【发布时间】:2018-08-09 20:26:39
【问题描述】:

这是我正在尝试开始工作的小脚本。它使用 cli 中的手刹来转换 avi。之后我尝试用 7zip cli 压缩文件,我不能使用 7zip powershell 包。如果我运行整个脚本,它将进入压缩部分并且只是出错。我尝试将它作为 cmd 运行并启动进程。就像它没有完成 foreach 而只是运行下一个命令。如果我自己运行它并运行一个已经转换的文件,则该命令有效。我在这里错过了什么?

$date = Get-Date -Format yyyy-MM-dd
    $filelist = Get-ChildItem C:\dan\VideoConvert\*.avi
     $i = 0

    ForEach ($file in $filelist)
    {
        $i++
        $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
        $newfile = $file.DirectoryName + "\converted$i.mp4";
        Start-Process "C:\dan\VideoConvert\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -o `"$newfile`"" -Wait
    }
    & "c:\dan\VideoConvert\7z.exe" a -t7z "$date" *.mp4
    #Start-Process "c:\dan\VideoConvert\7z.exe" -ArgumentList "a -t7z $date.7z *.mp4 -p"
    #Copy-Item c:\dan\VideoConvert\$date.7z \\ssb.local\shares\temp
    #Remove-Item c:\dan\VideoConvert\$date.7z

【问题讨论】:

标签: powershell


【解决方案1】:

在 Start-process 命令中,尝试包含 -wait 选项,以等待 zip 操作完成,然后再处理其输出。

提示:使用 7z.exe 开关 -mx0(根本不压缩),这会提高你的速度,因为它不会尝试进一步压缩已经压缩的 mp4 格式。

【讨论】:

    【解决方案2】:

    在 Roberts 评论的帮助下并使用 -mx=0 标志,我能够解决这个问题。谢谢!这是一份工作副本。

    $date = Get-Date -Format yyyy-MM-dd
    $filelist = Get-ChildItem C:\dan\VideoConvert\*.avi
    $pass = Read-Host -Prompt 'Enter Password for archive:  '
    $i = 0
    function CopyDelete ()
    {
        Copy-Item c:\dan\VideoConvert\"$date.7z" \\ssb.local\shares\temp
        Remove-Item c:\dan\VideoConvert\*.7z
        Remove-Item c:\dan\VideoConvert\*.mp4
    }
    function Process7z ()
    {
        Start-Process "c:\dan\VideoConvert\7z.exe" -ArgumentList "a -t7z -mx=0 C:\dan\VideoConvert\$date.7z c:\dan\VideoConvert\*.mp4 -p$pass" -wait
    }
    function ProcessVideo ()
    {
        ForEach ($file in $filelist)
        {
            $i++
            $oldfile = $file.DirectoryName + "\" + $file.BaseName + $file.Extension;
            $newfile = $file.DirectoryName + "\converted$i.mp4";
            Start-Process "C:\dan\VideoConvert\HandBrakeCLI.exe" -ArgumentList "-i `"$oldfile`" -o `"$newfile`"" -Wait -NoNewWindow
        }   
    }
    ProcessVideo
    Process7z
    CopyDelete
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-25
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多