命令失败时,Windows 7退出批处理文件

时间:2021-06-09 23:21:13

I'm writing a batch file on windows 7 that executing some commands one after another:

我正在Windows 7上编写一个批处理文件,它一个接一个地执行一些命令:

copy source1 dest1
call someFile1.bat
copy source2 dest2
call someFile2.bat
copy source3 dest3

Is there a way to return error code 1 if any of the commands returns error code 1?

如果任何命令返回错误代码1,有没有办法返回错误代码1?

Thanks!

1 个解决方案

#1


Yes, there is a way. The code must check the return code.

是的,有一种方法。代码必须检查返回码。

SET EXITCODE=0

copy source1 dest1
call someFile1.bat
if ERRORLEVEL 1 goto Failed
copy source2 dest2
call someFile2.bat
if ERRORLEVEL 1 goto Failed
copy source3 dest3
goto Success

:Failed
set EXITCODE=1
:Success
EXIT /B %EXITCODE%

If you want to check to see that the COPY commands work, that will be another if statement.

如果要检查COPY命令是否有效,那将是另一个if语句。

#1


Yes, there is a way. The code must check the return code.

是的,有一种方法。代码必须检查返回码。

SET EXITCODE=0

copy source1 dest1
call someFile1.bat
if ERRORLEVEL 1 goto Failed
copy source2 dest2
call someFile2.bat
if ERRORLEVEL 1 goto Failed
copy source3 dest3
goto Success

:Failed
set EXITCODE=1
:Success
EXIT /B %EXITCODE%

If you want to check to see that the COPY commands work, that will be another if statement.

如果要检查COPY命令是否有效,那将是另一个if语句。