Sunday, January 28, 2007

Inline Progress Bar

Here is a quick one to display inline precentage count (basic demo)

# sets the display two lines bellow the current cursor Y position

$CursorY = $host.UI.RawUI.CursorPosition.Y+2
write-host "`nprocessing something...`n" -fore yellow
1..100 | % {     
   [Console]::SetCursorPosition(0,$CursorY)     
   [Console]::Write("please wait... [ $_% ]")     
   sleep -m 50
}
write-host "`nProcessing something, is done.`n" -fore yellow

and for a "real-world" example:

$events = get-eventlog -logname system
$CursorY = $host.UI.RawUI.CursorPosition.Y+2
for($i -eq 0;$i -lt $events.count;$i++){     
   [int]$p=[math]::round(((100*$i)/$events.count))     
   [Console]::SetCursorPosition(0,$CursorY)     
   [Console]::Write("Percent complete: "+[String]::Format("{0:N0}{1}" ,$p,"%"))
}

write-host `n

$hay

3 comments:

/\/\o\/\/ said...

you can also use the write-progress Cmdlet for this e.g.:


1..100 |% {
write-progress "Search in Progress" "% Complete:" -perc $_
sleep -m 50
}

P.S. you lost the pipeline symbol in your post (blogger problem while editing )

Greetings /\/\o\/\/

$hay@israel said...

I forgot to mention the
write-progress cmdlet in the post.
What I tried to do (while experimenting), is to write my own version of it , one that lets me
replace the "look & feel" of write-progress, maybe even something like

=======[ "$_%" ]=======

I think write-progress wont let you
choose a replacement string to display.

As for blogspot HTML editor,
IT SUCKS BIG-TIME.
Prior to any post I have to double and double check the pipeline symbols.

Cheers!

/\/\o\/\/ said...

Hi $Hay,

Ok forget my comment then ;-)

about the Bloger Editor, yes that is the main reason I did switch to a new blog on Community server (very happy with it)