1

我有一个脚本,它为目录中的每个文件调用一个外部程序(SoX)。我Write-Progress在每次调用 SoX 之前调用,但是进度条被 SoX 的输出推离控制台缓冲区的顶部(不管控制台的大小)。我能做些什么来避免这种情况吗?

这是脚本:

$audioFiles = ls -Exclude *.ps1 | ? { !$_.PSIsContainer } 
foreach ($audioFile in $audioFiles)
{
    $i++
    Write-Progress -Activity "Transforming Audio" -Status $audioFile.Name -PercentComplete (($i / @($audioFiles).length) * 100)
    & 'C:\Program Files (x86)\sox-14-4-0\sox.exe' "$audioFile" ('Fast/' + $audioFile.Name) -S -G tempo -s 1.3 
}

Write-Progress -Activity "Transforming Audio" -PercentComplete 100 -Completed

[void] [Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) 
[windows.forms.messagebox]::show(“All done!”) 
4

1 回答 1

0

也许您可以在 sox 输出之前设置相同的控制台光标位置?请试试

$audioFiles = ls -Exclude *.ps1 | ? { !$_.PSIsContainer } 
foreach ($audioFile in $audioFiles){
    $i++
    Write-Progress -Activity "Transforming Audio" -Status $audioFile.Name -PercentComplete (($i / @($audioFiles).length) * 100)

    # get windows height 
    $y=[int]($host.ui.rawui.WindowSize.Height -5)
    # will set cursor position to bottom of the screen 
    $Host.UI.RawUI.CursorPosition = New-Object System.Management.Automation.Host.Coordinates 2,$y 
    #clear current line
    $sbOut = new-object System.Text.Stringbuilder
    (0.. $Host.UI.RawUI.WindowSize.Width)|%{$sbOut.append(' ')} |out-Null
    write-Host $sbOut.toString() -NoNewline

    & 'C:\Program Files (x86)\sox-14-4-0\sox.exe' "$audioFile" ('Fast/' + $audioFile.Name) -S -G tempo -s 1.3 
    }
Write-Progress -Activity "Transforming Audio" -PercentComplete 100 -Completed
[void] [Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) 
[windows.forms.messagebox]::show(“All done!”)

您还可以将 sox 输出重定向到文件,或者如果您不关心 sox 输出,只需将其重定向到 out-null :

& 'C:\Program Files (x86)\sox-14-4-0\sox.exe' "$audioFile" ('Fast/' + $audioFile.Name) -S -G tempo -s 1.3 | out-null
于 2012-11-20T07:48:47.193 回答