如何在调用循环中完全停止批处理文件?在“call:loop”中完全停止批处理文件
exit /b
仅仅退出:标签循环,而不是整个批处理文件,而裸exit
离开批处理文件和父CMD壳,这是不希望的。
@echo off
call :check_ntauth
REM if check fails, the next lines should not execute
echo. ...About to "rmdir /q/s %temp%\*"
goto :eof
:check_ntauth
if not `whoami` == "nt authority\system" goto :not_sys_account
goto :eof
:not_sys_account
echo. &echo. Error: must be run as "nt authority\system" user. &echo.
exit /b
echo. As desired, this line is never executed.
结果:
d:\temp>whoami
mydomain\matt
d:\temp>break-loop-test.bat
Error: must be run as "nt authority\system" user.
...About to "rmdir /q/s d:\temp\systmp\*" <--- shouldn't be seen!
棒极了!下次我需要时,我必须尝试一下。我通常设置一个变量开头,如:**'fatalError = 0' **,然后将其设置为** fatalError = 1 **,并检查返回值:calls。 –
我喜欢这个,感谢这个想法。我最终给了乔恩点头,因为这种方法对我来说会更清洁,因为未来会有其他人维护。 –