首页 > 解决方案 > 当另一个失败时我如何运行命令(BATCH)

问题描述

如果命令失败,我会尝试运行错误消息,但我不知道command1 && command2如果您希望仅在第一个命令成功时才运行第二个命令但我不知道如何批量执行此操作,我怎么知道您可以使用。我发现的东西也不起作用command1 | command2

我现在正在尝试这个for %%i in (*.osz) do start "" "%%~i | echo Error accrued! && Color c && timeout -1 >nul",但它不起作用。

请帮助我

标签: batch-file

解决方案


您应该寻找“errorlevel”,您将能够测试您的命令返回的错误代码: https ://stackoverflow.com/a/6817833/6424355

command
if errorlevel 1 goto errorHandling
goto successHandling

在循环中,您可以使用 call 调用批处理的子部分:likeAGotoButYouWillGetBackHere

for ... call :likeAGotoButYouWillGetBackHere
goto eof
:likeAGotoButYouWillGetBackHere
...
goto eof   

推荐阅读