我在批处理文件中调用命令的两行是这样的:如何在批处理文件中有条件地编写调用命令?
call execute.cmd
call launch.cmd
我需要的是调用launch.cmd当且仅当调用execute.cmd成功。 那么有什么方法可以在这里提出一些条件?
execute.cmd在此处不返回任何值。
我在批处理文件中调用命令的两行是这样的:如何在批处理文件中有条件地编写调用命令?
call execute.cmd
call launch.cmd
我需要的是调用launch.cmd当且仅当调用execute.cmd成功。 那么有什么方法可以在这里提出一些条件?
execute.cmd在此处不返回任何值。
如果execute.cmd
返回一个整数比,你可以使用一个IF command
检查它的返回值,如果它符合期望的人比你可以打电话launch.cmd
假设execute.cmd
返回0,如果它是成功或整数> =否则为1。该批次是这样的:
rem call the execute command
call execute.cmd
rem check the return value (referred here as errorlevel)
if %ERRORLEVEL% ==1 GOTO noexecute
rem call the launch command
call launch.cmd
:noexecute
rem since we got here, launch is no longer going to be executed
注意,rem
命令用来征求意见。
HTH,
JP
我相信这是How do I make a batch file terminate upon encountering an error?重复。
你这里的解决办法是:
call execute.cmd
if %errorlevel% neq 0 exit /b %errorlevel%
call launch.cmd
if %errorlevel% neq 0 exit /b %errorlevel%
不幸的是,它看起来像Windows批处理文件没有等同的UNIX bash的set -e
和set -o pipefail
。如果您愿意放弃非常有限的批处理文件语言,则可以尝试Windows PowerShell。
谢谢,你也是.. – Anand
exceute.cmd如果成功返回0,如果不成功则返回1,所以问题用你的逻辑解决了,非常感谢。正如你首先回答,我接受你的答案,尽管@jhclark的回答看起来是正确的。 – Anand
@Anand K Gupta your wellcome :) –
您也可以通过这种方式使用Windows Batch &&组合实现同样的功能:call execute.cmd && call launch.cmd – Aacini