0

I want to append an extra bit of text behind an already executed echo command. For example "Done" or "Failed" based on if something went well or not.

I am trying to create a batch file that checks if a file exists, then proceeds to the next step. I admit I'm not as good with batch, My current solution is, using goto statements, printing a new line with either if it went well or not.

Current solution:

@echo off
cls
echo Checking if the file is present...

if exist "file" goto :Next
color 4
echo File not found!
pause
exit

:Next
color a
echo Done.
:: Rest of the script continues here.

What I'm trying to do:

@echo off
cls
echo Checking if the file is present...

if exist "file" goto :Next
:: Here it should append "File not found!" at the end of the previous echo
pause
exit

:Next
:: Here it should append "Done." at the end of the previous echo

:: Rest of the script continues here.

The expected result would in simple terms allow something at the end of an a line I previously output via echo, to be appended at the end, preferably with the colors (here I used red for failed, and green for success). Is this possible at all? If so, how would I go about doing it?

Please note, there is probably better ways to lay out my script, as mentioned I'm not good with batch.

Any help would be appreciated!

  • One way to accomplish this would be to use something like this: set x=Hello set x=%x% World! echo %x% – hsbatra Oct 29 '22 at 05:49

2 Answers2

3

(ab)use set /p to write without a linefeed:

@echo off
<nul set /p "=working...  "
timeout 3 >nul
echo done.
Stephan
  • 53,940
  • 10
  • 58
  • 91
-1

You can use -n for first command. It will not add newline for after echo.

echo -n "Checking if the file is present..."

Avi
  • 11
  • 3