批量在 echo 命令末尾追加一些东西

Append something at the end of an echo command in batch

我想在已执行的 echo 命令后面附加一些额外的文本。例如 "Done" 或 "Failed" 取决于事情是否顺利。

我正在尝试创建一个批处理文件来检查文件是否存在,然后继续下一步。我承认我不太擅长批处理,我目前的解决方案是,使用 goto 语句,无论是否顺利,都打印一个新行。

当前解决方案:

@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.

我想做什么:

@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.

简单来说,预期结果将允许在我之前通过 echo 输出的一行末尾附加一些内容,最好是带有颜色(这里我使用红色表示失败,绿色表示成功).这可能吗?如果是这样,我将如何去做?

请注意,可能有更好的方法来布置我的脚本,如前所述,我不擅长批处理。

如有任何帮助,我们将不胜感激!

您可以对第一个命令使用-n。它不会在回显后添加换行符。

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

(ab)使用 set /p 写而不换行:

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