如何在調用循環中完全停止批處理文件?在「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。 –
我喜歡這個,感謝這個想法。我最終給了喬恩點頭,因爲這種方法對我來說會更清潔,因爲未來會有其他人維護。 –