Search code examples
batch-fileappendecho

Append something at the end of an echo command in batch


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!


Solution

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

    @echo off
    <nul set /p "=working...  "
    timeout 3 >nul
    echo done.