2014-11-03 150 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