写入进度显示在控制台缓冲区之外

时间:2012-11-20 03:28:17

标签: powershell

我有一个脚本,它为目录中的每个文件调用外部程序(SoX)。我在每次调用SoX之前调用Write-Progress,但是进度条被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!”) 

1 个答案:

答案 0 :(得分: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