2014-11-03 148 views
0

到目前爲止,人們在這裏已經很棒了。完成搜索(儘管總是不夠廣泛!)。爲什麼這不起作用?if [true] and [true] then .... else .... batch

if [%_dpsUserUpdate%] == [false] if [%_dpsUserPrompt%] == [false] (
    echo Both User Update and Prompt set to false. Run 7z silently..... 
    7z e "%_file%" -y > nul 
) else (
    echo Either User Update and/or Prompt set to true. Run 7z gui..... 
    7zG e "%_file%" 
) 

目標 - 如果Update和Prompt都設置爲false,則運行7z。否則(對於其餘3個排列)運行7zG。適用於「假和假」,但不適用於其他3種組合......

當然,我可以在那裏貼一個「goto」,但總是感覺像'壞'編碼(不知道爲什麼!)。

+0

[如何在批處理文件中使用if-else結構?](http://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file) – 2014-11-03 23:15:55

+0

嘗試'if [%_dpsUserUpdate%] [%_ dpsUserPrompt%] == [false] [false]('。 .. – JosefZ 2014-11-03 23:23:52

+0

您需要準確顯示您如何設置'_dpsuserupdate'和'_dpsuserprompt'。 – Magoo 2014-11-04 00:05:11

回答

0

您的代碼無法按預期工作,因爲IF命令的語法如下:if condition command。這樣,你有兩個 IF命令和只有一個ELSE子句,所以它像往常一樣適用於最後的 IF命令。換句話說,你的例子是等效於此:

if [%_dpsUserUpdate%] == [false] (
    if [%_dpsUserPrompt%] == [false] (
     echo Both User Update and Prompt set to false. Run 7z silently..... 
     7z e "%_file%" -y > nul 
    ) else (
     echo Either User Update and/or Prompt set to true. Run 7z gui..... 
     7zG e "%_file%" 
    ) 
) 

我覺得這種方式更清晰的看到:

if [%_dpsUserUpdate%] == [false] (
    echo User Update is false 
    if [%_dpsUserPrompt%] == [false] (
     echo Both User Update and Prompt set to false. 
    ) else (
     echo User Update is false, Prompt is true 
    ) 
) else (
    echo User Update is true 
    if [%_dpsUserPrompt%] == [false] (
     echo User Update is true. Prompt is false 
    ) else (
     echo User Update is true. Prompt is true 
    ) 
) 

這是我會做的方式:

set "bothAreFalse=true" 
if [%_dpsUserUpdate%] neq [false] set "bothAreFalse=" 
if [%_dpsUserPrompt%] neq [false] set "bothAreFalse=" 
if defined bothAreFalse (
    echo Both are false 
) else (
    echo Anyone of the other three cases 
) 
+0

我知道你並不是想要說「謝謝。」但是謝謝!:)澄清 – stigzler 2014-11-04 22:22:38

+0

你可能說「謝謝」,當你有足夠的代表時,提出我的答案...... **';-)'** – Aacini 2014-11-05 00:59:16