if [true]和[true] then .... else .... batch

时间:2014-11-03 22:27:49

标签: batch-file if-statement

到目前为止,人们在这里一直都很棒。完成搜索(虽然总是不够广泛!)。为什么这不起作用?

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"在那里,但总是感觉像是'坏''编码(不知道为什么!)。

1 个答案:

答案 0 :(得分:0)

您的代码无法正常工作,因为IF命令的语法是:if condition command。这样,您有两个 IF命令和一个ELSE子句,因此它像往常一样应用于 last 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
)
相关问题