Solution: to write multiple times on the same line
Posted: 03 Apr 2010 14:08
Hello,
the set /p command has some interessting features.
The normal usage is to display a text and wait for an answer, finished by <Enter>.
But you can use it also to display only a text like
or the same with an other notation
Ok. But that seems to be the same like the echo command.
But not excactly, the set /p variant does not append the CR/LF at the end of the line.
So there are different results for
This is enough for a simple progress bar.
But with a further trick you can use it to solve an other type of problem.
Count a number on a line, or generally replace text on a line, without clearing the whole screen.
You only need to print a CR (0x0d) character, to move the cursor again at the first position of the line.
Ok, you need a single CR character. But this can be done with a file.
This works fine ... on XP, but not under Vista anymore.
It's because Vista have implemented a "feature" for the set /p, stripping all white space (space, tab, and CR) behind the equal sign.
But only the front will stripped, not the end.
But there is a work around, place the CR at the end or after a non space character
Attention:
After the equal sign it's not allowed to place direct an other equal sign (after stripping the whate spaces)
This will not work, and results in an error.
Finally:
hope it helps
Jan Erik
the set /p command has some interessting features.
The normal usage is to display a text and wait for an answer, finished by <Enter>.
Code: Select all
set /p user=Enter your name?
But you can use it also to display only a text like
Code: Select all
set /p someVar=Display this text < nul
or the same with an other notation
Code: Select all
<nul set /p "=Display this text"
Ok. But that seems to be the same like the echo command.
But not excactly, the set /p variant does not append the CR/LF at the end of the line.
So there are different results for
Code: Select all
echo Line1
echo Line2
result:
Line1
Line2
Code: Select all
<nul set /p ".=Line1"
<nul set /p ".=Line2"
result:
Line1Line2
This is enough for a simple progress bar.
But with a further trick you can use it to solve an other type of problem.
Count a number on a line, or generally replace text on a line, without clearing the whole screen.
You only need to print a CR (0x0d) character, to move the cursor again at the first position of the line.
Ok, you need a single CR character. But this can be done with a file.
Code: Select all
@echo off
setlocal
setlocal enabledelayedexpansion
:: Assign a single CR to a variable, use a textfile with 2 bytes of 0x0d, 0x0d
for /f "tokens=1" %%x in ('type cr.txt') do set CR=%%x
::Simple sample, count from 1 to 2000, print it on the same line
for /L %%c in ( 1,1,2000) do (
<nul set /p ".=!CR!Count %%c"
)
This works fine ... on XP, but not under Vista anymore.
It's because Vista have implemented a "feature" for the set /p, stripping all white space (space, tab, and CR) behind the equal sign.
But only the front will stripped, not the end.
Code: Select all
<nul set /p ".= Hello "
<nul set /p ".=two"
result XP:
<space>Hello two
result Vista
Hello two
But there is a work around, place the CR at the end or after a non space character
Code: Select all
<nul set /p ".=Count %%c!CR!"
or
<nul set /p ".=_!CR!Count %%c"
Attention:
After the equal sign it's not allowed to place direct an other equal sign (after stripping the whate spaces)
This will not work, and results in an error.
Code: Select all
set /p ".= ="
Finally:
Code: Select all
@echo off
setlocal
setlocal enabledelayedexpansion
:: Assign a single CR to a variable, use a textfile with 2 bytes of 0x0d, 0x0d
for /f "tokens=1" %%x in ('type cr.txt') do set CR=%%x
::Simple sample, count from 1 to 2000, print it on the same line
for /L %%c in ( 1,1,2000) do (
<nul set /p ".=Count %%c!CR!"
)
hope it helps
Jan Erik