【问题标题】:Powershell: Update current output linePowershell:更新当前输出线
【发布时间】:2013-09-10 17:19:25
【问题描述】:

有没有办法在powershell中更新命令输出的当前行?

我知道如何写到同一行:

Write-Host "hello " -NoNewLine

Write-Host "world!" -NoNewLine

但是有没有办法完成替换当前行?类似:

Write-Host "hello "
Write-Host "world!" -ReplaceLine

提前致谢!


解决方案:

Write-Host "hello " -NoNewLine
Write-Host "`rworld!"

【问题讨论】:

    标签: powershell command-line output


    【解决方案1】:

    使用回车返回行首:`r

    看到这个:http://ss64.com/ps/syntax-esc.html

    及回车定义:http://en.wikipedia.org/wiki/Carriage_return

    如果你想清除一行,那还有一大堆空格。否则你可以覆盖东西。

    我不能在这里写一个例子,因为它是 SO 的代码字符,但把它放在“世界”字符串的开头,它应该可以工作。

    希望这会有所帮助!

    【讨论】:

    • 感谢 Mattsjo 的成功!我已经用你的回答更新了我的问题!
    • 这似乎不再起作用了。我错过了什么吗?啊,似乎可以在控制台中工作,但不能在 ISE 中工作。
    【解决方案2】:

    正如其他人所指出的,解决方案是在写入主机时使用回车。 确保使用NoNewLine 标志和write-host 否则它将不起作用。

    回车是通过使用:`r

    这是一个说明这个答案的小例子:

    $up = $true
    $finish = $false
    $timeout = 50
    Write-Host "Updating" -NoNewline
    do {
        for ($i=0;$i -le 100;$i++){
            if ($i -eq 100){$up = !$up}
            if ($up){
                $str = ""
                $x=99-$i
                for($z=0;$z -le $i;$z++){$str+="."}
                for($y=0;$y -le $x;$y++){$str+=" "}
                Write-host "`rUpdating$str" -NoNewline
                Start-Sleep -Milliseconds $timeout
            }else{
                $str = ""
                $x=99-$i
                for($y=0;$y -le $x;$y++){$str+="."}
                for($z=0;$z -le $i;$z++){$str+=" "}
                Write-host "`rUpdating$str" -NoNewline
                Start-Sleep -Milliseconds $timeout
            }
        }
        if ($timeout -le 0){$finish = $true}
        $timeout-=10
    } until ($finish)
    $str = ""
    for ($i=0;$i -le 93;$i++){$str+=" "}
    Write-Host "`rUpdate Complete!$str"
    Read-Host "Press [ENTER] to close this Window"
    

    【讨论】:

      猜你喜欢
      • 2013-03-01
      • 2020-04-27
      • 2013-05-02
      • 2018-09-28
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多