退出调用另一个批处理脚本的批处理脚本

时间:2014-10-08 17:43:02

标签: windows batch-file cmd call parent

我有一个调用一系列批处理文件的批处理脚本。

出现错误情况,我需要退出调用的批处理文件以及父批处理文件。是否可以在子批处理文件中完成此操作?

test.bat的

rem Test1.bat will exit with error code.
call test1.bat
rem Want script to stop if test1.bat errors.
call test2.bat

test1.bat

rem Can I get test.bat to terminate from inside test1.bat?
exit /b 1

2 个答案:

答案 0 :(得分:2)

您可以使用errorlevel。如果被叫批处理系统地使用exit 0通知继续exit 1要求来电者停止,您可以通过以下方式修改来电者:

rem Test1.bat will exit with error code.
call test1.bat

rem Want script to stop if test1.bat errors.
if errorlevel 1 goto fatal
call test2.bat
exit 0
:fatal
echo Fatal error
exit 1

答案 1 :(得分:0)

您可以通过在子级中引发致命的语法错误来退出子级的所有子级和父级批处理:

test.bat

@echo off
echo before
call test1.bat
echo after

test1.bat

@echo off
echo in test 1
call :kill 2>nul

:kill - Kills all batch processing with a fatal syntax error
() rem fatal syntax error kills batch and all parents

呼叫test.bat会打印“之前”和“测试1”,而不是“之后”。