You already set an exit code (errorlevel).
(您已经设置了退出代码(错误级别)。)
Just react on it: (对此做出反应:)
setlocal enabledelayedexpansion
For %%A in (alpha beta gamma) DO (
Echo Outer loop %%A
Call :inner
if !errorlevel! equ 2 (echo inner loop failed & exit /b 2)
)
Goto :eof
:inner
For %%B in (U V W X Y Z) DO (
if %%B==X ( exit /b 2 )
Echo Inner loop Outer=%%A Inner=%%B
)
exit /b 1
Aschipfl suggested " Just insert if ErrorLevel 2 goto :EOF
after the call
command line "
(Aschipfl建议“ if ErrorLevel 2 goto :EOF
在call
命令行后在if ErrorLevel 2 goto :EOF
插入 ”)
That might be a good idea (and avoids delayed expansion), but be aware that if errorlevel 2
actually means "if errorlevel is 2 or higher".
(这可能是一个好主意(并避免延迟扩展),但是请注意, if errorlevel 2
实际上表示“如果错误级别为2或更高”。)
If your inner loop only ever returns 0
or 2
, that's definitively the better solution. (如果您的内部循环仅返回0
或2
,那肯定是更好的解决方案。)
When it may return more possible errorlevels, you have to use extreme care handling them with if errorlevel
. (当它可能返回更多可能的错误级别时,您必须格外小心处理if errorlevel
。)
(That's the reason, I chose if !errorlevel!
instead) ((这就是原因,我选择的if !errorlevel!
))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…