Echoing in the same line
Solution 1
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"
Solution 2
Try this on Posix system (Linux)
echo -n "Executing backup.... "
echo -n "backup procedure "
echo "Ok"
It is much harder on Windows. You will need to use something like this:
@echo off
echo|set /p ="Executing backup...."
echo|set /p =" backup procedure"
Check this post: http://www.pcreview.co.uk/forums/showpost.php?s=1a20b16775d915998b30bd76a0ec5d35&p=4432915&postcount=7.
Solution 3
It's a bit of a hack, but here is an article describing how to do it for Windows.
From the article, the final result (edited for your setup) looks like this:
SET /P var=Backing up
%Result%....<NUL
Backup_process %Result% >NUL 2>&1
IF ERRORLEVEL 1
ECHO FAIL
ELSE
ECHO OK
Admin
Updated on June 05, 2022Comments
-
Admin almost 2 years
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?!
-
Admin over 13 yearsThe first example you wrote works perfetly. What is the sign <nul for?
-
jeb over 13 years<nul is for piping nothing to set /p, so it doesn't wait for user input
-
user66001 over 11 yearsPutting <nul at the end (i.e.,
set /p "=Executing backup...." <nul
) seems to work the same and wouldn't have (at least) thrown me off if I saw it in this position, so have suggested an edit to this post. -
user66001 over 11 years@jeb - Interesting(!) counter example. While I can follow most of it, not sure about a couple of things - 1) Once processing commands post the label CreateCR, how does it return to the command post the call to the label CreateCR, 2) What is contained in the var cr that causes the next set /p output to not sit alongside the previous. I think I can see that the for loop setting X just exponentially increases the number of .'s in %temp%\cr.tmp, but then don't see how the \ and using it as the delimiter in the next for loop works. Perhaps a full step by step rundown would be appreciated by others?
-
user66001 over 11 years@Piotr Czapla - I don't believe the / after each set (Windows example) is required, or at least it seems to work the same without. However, upon trying to remove them, someone prevented the edit, so perhaps this is wrong stackoverflow.com/questions/13665411/…. Can you advise the need for the /'s?
-
jeb over 11 years@user66001 1) A
goto :eof
works like areturn
here 2) I append an explanation -
user66001 over 11 yearsP.S Seems call and goto have added functionality when setlocal EnableExtensions is used (microsoft.com/resources/documentation/windows/xp/all/proddocs/… & microsoft.com/resources/documentation/windows/xp/all/proddocs/…)
-
user66001 over 11 yearsdostips.com was useful in figuring out what I could of deb's very cunning code
-
user66001 over 11 years@jeb - Also, 1) is "rem setlocal EnableDelayedExpansion EnableExtensions" necessary?, 2) How does the string manipulation create 8188 dots, when (I am guessing) the expanded expression string manipulation from 0, to 4094 is repeating the . by 4094 x 2, but the loop is running 13 times?, 3) What purpose does usebackq have in this? 4) Can't %temp%\cr.tmp just contain
<8188 .'s><SPACE><CR><CR><LF>
as it seems substituting this into the code, puts<CR><CR><LF>
in the second token, 5) Does theset cr=
, orset /p
, or something else, remove the lastCR/LF
as standard line endings? -
user66001 over 11 years6) After reading the above, I can understand the goto :EOF's that aren't indented, but not the ¿extra? one in the for loop, as surely this would make the batch file exit (Being the two talked about being required in the URL's above, without the final below the set /P? 7) Perhaps there should be a test to make sure var CR doesn't already exist, to prevent unncessary recreation of it? 8) What is contained in the var cr that causes the next set /p output to not sit alongside the previous (i.e., Return the cursor to the start of the line?
-
SomethingDark almost 8 yearsBecause you're four years late and have the exact same solution as the accepted answer.
-
George Birbilis almost 8 yearsare you serious? that answer I sent here is from another thread. The accepted answer here is some very long text instead
-
SomethingDark almost 8 yearsWhich redirects
set /p
tonul
in the first code block. So yeah, it's the same solution. -
George Birbilis almost 8 yearsyeah sure, together with a ton of other text - anyway I was mostly interested in the info about the behavior of the pipe there when i had copy-pasted that text
-
George Birbilis almost 8 yearsthe pipe creates new cmd.exe instances is mentioned at other related thead (see my reply)