Echoing in the same line
Asked Answered
J

5

9

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?!

Jeffreys answered 4/11, 2010 at 13:17 Comment(2)
Which operating system are you on?Abstract
yes, windows server 2003, I forgot...Jeffreys
R
11

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"
Royo answered 4/11, 2010 at 13:33 Comment(9)
The first example you wrote works perfetly. What is the sign <nul for?Jeffreys
<nul is for piping nothing to set /p, so it doesn't wait for user inputRoyo
Putting <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.Purdum
@Royo - 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?Purdum
@Purdum 1) A goto :eof works like a return here 2) I append an explanationRoyo
P.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/…)Purdum
dostips.com was useful in figuring out what I could of deb's very cunning codePurdum
@Royo - 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 the set cr=, or set /p, or something else, remove the last CR/LF as standard line endings?Purdum
6) 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?Purdum
M
5

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.

Madelainemadeleine answered 4/11, 2010 at 13:21 Comment(2)
@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 #13665911. Can you advise the need for the /'s?Purdum
the pipe creates new cmd.exe instances is mentioned at other related thead (see my reply)Hutcheson
A
1

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
Abstract answered 4/11, 2010 at 13:26 Comment(0)
L
0

I've done something similar using a VBScript.

Put this code in EchoNoNewline.vbs:

If WScript.Arguments.Named.Exists("TEXT") Then
  WScript.StdOut.Write WScript.Arguments.Named.Item("TEXT")
End If

From your batch file, use the script like this:

CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Executing backup...."
backup procedure
CSCRIPT EchoNoNewLine.vbs //NOLOGO /TEXT:"Ok"
Luciano answered 4/11, 2010 at 14:57 Comment(0)
H
0

at What does a forward slash before a pipe in cmd do to remove the line ending of an echo? the best suggestion is:

to echo text without a linefeed is very inefficient, as a pipe creates two new instances of cmd.exe. It's much simpler and faster to use

     <nul set /p "=My Text"

The redirect from NUL will also stop the waiting for user input.

Hutcheson answered 27/3, 2014 at 10:25 Comment(4)
Because you're four years late and have the exact same solution as the accepted answer.Potency
are you serious? that answer I sent here is from another thread. The accepted answer here is some very long text insteadHutcheson
Which redirects set /p to nul in the first code block. So yeah, it's the same solution.Potency
yeah 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 textHutcheson

© 2022 - 2024 — McMap. All rights reserved.