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

phuclv
  • 37,963
  • 15
  • 156
  • 475

5 Answers5

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"
user66001
  • 774
  • 1
  • 13
  • 36
jeb
  • 78,592
  • 17
  • 171
  • 225
  • The first example you wrote works perfetly. What is the sign –  Nov 04 '10 at 14:49
  • @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 Dec 02 '12 at 01:18
  • @user66001 1) A `goto :eof` works like a `return` here 2) I append an explanation – jeb Dec 03 '12 at 14:11
  • 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/…) – user66001 Dec 03 '12 at 20:57
  • http://www.dostips.com/ was useful in figuring out what I could of deb's very cunning code – user66001 Dec 03 '12 at 20:59
  • @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>` as it seems substituting this into the code, puts `` in the second token, 5) Does the `set cr=`, or `set /p`, or something else, remove the last `CR/LF` as standard line endings? – user66001 Dec 03 '12 at 21:02
  • 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? – user66001 Dec 03 '12 at 21:04
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.

user66001
  • 774
  • 1
  • 13
  • 36
Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
  • @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 http://stackoverflow.com/questions/13665411/what-does-a-forward-slash-before-a-pipe-in-cmd-do-to-remove-the-line-ending-of-a#comment18755645_13665411. Can you advise the need for the /'s? – user66001 Dec 02 '12 at 02:25
  • the pipe creates new cmd.exe instances is mentioned at other related thead (see my reply) – George Birbilis Jun 23 '16 at 01:35
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
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Gary
  • 7,167
  • 3
  • 38
  • 57
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.

Community
  • 1
  • 1
George Birbilis
  • 2,782
  • 2
  • 33
  • 35
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"
aphoria
  • 19,796
  • 7
  • 64
  • 73