4

I have a simple file munging utility, and I would like to provide some progress feedback without creating a "scroll storm" on the console. I tried this:

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()
}

I get output which looks like this:

Processing line #:  1 Processing line #:  2 Processing line #:  3 ...

It appears that the carriage return is not recognized or is stripped out. Is there any way to get around this with Write-Host?

If not, what would a PowerShell newbie do to overwrite the last line written to the console?

1
  • This is only a problem in PowerShell ISE. When I ran the program using powershell from the command line, the carriage return was processed as expected. Commented Jan 28, 2014 at 19:08

2 Answers 2

1

Try this:

gci *.txt |% {write-host "`r$($_.name)" -NoNewline; Start-Sleep -Seconds 1}
Sign up to request clarification or add additional context in comments.

1 Comment

No, I get pretty much the same results as my original solution in ISE. I'm thinking it just can't be done.
1

Modify the Write-Host line as follows:

Write-Host "`rProcessing line #: $lineNo" -NoNewline

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.