我有一个简单的文件munging实用程序,我想提供一些进度反馈而不在控制台上创建“滚动风暴”。我试过这个:
param([string] $input = "", [string] $output = "")
$source = $PSBoundParameters["input"]
$destination = $PSBoundParameters["output"]
if (!$source)
{exit}
if (!$destination)
{
$destination = [System.IO.Path]::GetDirectoryName($source) `
+ "\fmt_" `
+ [System.IO.Path]::GetFileName($source)
}
$reader = [System.IO.File]::OpenText($source)
$writer = [System.IO.file]::CreateText($destination)
$lineNo = 0
try {
for(;;) {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
$lineNo++
Write-Host "Processing line #:" $lineNo -NoNewline "`r"
#process the line...
$writer.Writeline($line)
}
}
finally {
$reader.Close()
$writer.Close()
}
我得到的输出如下:
Processing line #: 1 Processing line #: 2 Processing line #: 3 ...
看来回车无法识别或被剥离。有没有办法解决这个与Write-Host?
如果没有,PowerShell新手会怎样覆盖写入控制台的最后一行?
答案 0 :(得分:1)
试试这个:
gci *.txt |% {write-host "`r$($_.name)" -NoNewline; Start-Sleep -Seconds 1}
答案 1 :(得分:1)
修改Write-Host行,如下所示:
Write-Host "`rProcessing line #: $lineNo" -NoNewline