Arthur
Arthur

Reputation: 41

How do I echo strings on the same line as a previous echo?

How do I echo "I have 1 number" using two echo commands in a batch file?

echo "I have "
...
... do stuff
...
echo "1 number"

creates two lines:

I have
1 number

how do I suppress the cr/lf on the first echo? to create:

I have 1 number

Upvotes: 0

Views: 3430

Answers (2)

lit
lit

Reputation: 16236

Perhaps I am missing part of the problem. Save the first part in a variable, then use it later with the latter part.

SET "PART_1=I have "
.
... do stuff
.
ECHO %PART_1% 1 number

How about...

C:\Users\lit\t>ECHO SET "currentdir=%CD%" >a.bat

C:\Users\lit\t>type a.bat
SET "currentdir=C:\Users\lit\t"

Multiple lines...? The ECHO command writes a newline CRLF at the end of anything it outputs. You are not going to get a subsequent ECHO command to write over the newline from the first ECHO. How about...

SET TMPFILE=%TEMP%\mytempfile.tmp
ECHO>"%TMPFILE%" I have
... do stuff
FOR /F "usebackq tokens=*" %%a IN (`TYPE "%TMPFILE%"`) DO (SET "S1=%%~a")
ECHO %S1% 1 file
IF EXIST "%TMPFILE%" (DEL "%TMPFILE%")

Upvotes: 0

Arthur
Arthur

Reputation: 41

this appears to do it:

<nul set /p=I have 
echo 1 number

note that this example is too simplistic- in a more general case the second part is not just an echo but the result of some calculation or manipulation, for example cd:

<nul set /p=set currentdir=>a.bat
cd>>a.bat

I can't see that this functionality is possible with the PART1 answer given above.

Upvotes: 1

Related Questions