我在批處理文件中調用命令的兩行是這樣的:如何在批處理文件中有條件地編寫調用命令?
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