Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

windows - Echoing in the same line

I had a look at the previous questions of your db and I didn't try an answer, but I try.

I would like to write the following lines code:

echo Executing backup....
backup procedure
echo Ok

but the output should be:

Executing backup... Ok

That's possible?!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suppose you are using dos/nt-batch.

It is possible with the set /p command, because set /p doesn't print a CrLf

set /p "=Executing backup...." <nul
echo OK

Also it's possible to erase the line with a CR character. It's important to know that whitespace characters at the front of an set /p are ignored (in Vista, not in XP), so the !cr! has to placed later or at the end. A CR can only be displayed with delayedExpansion, because %cr% works, but CR characters are removed in the percent expansion phase(or directly after this phase), but not in the delayed expansion phase.

Example of a counter which use only one line for displaying

@echo off
setlocal EnableDelayedExpansion EnableExtensions


call :CreateCR
for /l %%n in (1,1,10000) do (
    set /P "=Count %%n!CR!" <nul
)
echo(
goto :eof

:CreateCR
rem setlocal EnableDelayedExpansion EnableExtensions
set "X=."
for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!

echo !X!  > %temp%cr.tmp
echo>> %temp%cr.tmp
for /f "tokens=2 usebackq" %%a in ("%temp%cr.tmp") do (
   endlocal
   set cr=%%a
   goto :eof
)
goto :eof

EDIT: Explanation, how the variable cr is created (Done with a trick)

After setting variable X to a single dot (the character itself is unimportant), it is repeated to become 8188 characters by way of for /L %%c in (1,1,13) DO set X=!X:~0,4094!!X:~0,4094!

Then the variable, two spaces and both a CR and LF are echoed into a file with echo !X! > %temp%cr.tmp (Notice the two spaces between the !X! and the > and the natural line endings echo amends internally)

We now have 8192 characters, but the data buffer can only hold 8191 characters, so the last character (the linefeed) will be dropped!

In the next line echo>> %temp%cr.tmp, another CR/LF set is appended to the file (the in the command is just to output nothing bar the carriage return and line feed, as echo by it's self will output ECHO is ON/OFF), that's important, as a single CR can't be read at the end of a line (More later).

So the file now contains <8188 .'s><SPACE><SPACE><CR><CR><LF>

The for /f "tokens=2 usebackq" %%a in ("%temp%cr.tmp") do reads the second token, the delimters are standard space and tab, so the second token is only a single CR, as the following CR/LF is removed as standard line ending.

Finally the endlocal is used to return to an environment without the temporary variables X, c and a existing (As with the endlocal in brackets, it allows the setting of cr before the endlocal actually takes affect at the end of the brackets (could also be written as for /f "tokens=2 usebackq" %%a in ("%temp%cr.tmp") do endlocal&set cr=%%a&goto :eof)

Additionally

This was my first way to create a CR character, but it needs some time and a temporary file.
Later I saw a simpler method of retrieving the CR from a copy /z command.

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...