0

People have been great on here so far. Done searching (although invariably not extensively enough!). Why doesn't this work?

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%"
)

Aim - if Update and Prompt both set to false, run 7z. Otherwise (for remaining 3 permutations) run 7zG. Works for "false and false" but not for other 3 combinations....

Of course, i could stick a "goto" in there, but that always feels like 'bad' coding (dunno why!).

stigzler
  • 793
  • 2
  • 12
  • 29
  • possible duplicate of [How to use if - else structure in a batch file?](http://stackoverflow.com/questions/11081735/how-to-use-if-else-structure-in-a-batch-file) – Ken White Nov 03 '14 at 23:15
  • Try `if [%_dpsUserUpdate%][%_dpsUserPrompt%] == [false][false] (` ... – JosefZ Nov 03 '14 at 23:23
  • You would need to show exactly how you are setting `_dpsuserupdate` and `_dpsuserprompt`. – Magoo Nov 04 '14 at 00:05
  • And you can force string comparisons to ignore case with **/i** switch: `if /i [%_dpsUserUpdate%][%_dpsUserPrompt%] == [false][false] ( `... – JosefZ Nov 04 '14 at 01:10
  • Thanks Josef. Above produced a break. Command line: [true] was unexpected at this time. C:\[blah]>if [false] [true] == [false] [false] ( c:\[blah]> Magoo - just straight set, i.e.: set _dpsUserUpdate=true vars checked - no trailing spaces or such nonsense. – stigzler Nov 04 '14 at 06:49

1 Answers1

1

Your code does not works as intended because the syntax of IF command is this: if condition command. This way, you have two IF commands and just one ELSE clause, so it is applied to the last IF command as usual. In other words, your example is equivalent to this one:

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%"
    )
)

I think this way is clearer to see:

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
    )
)

This is the way I would do it:

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
)
Aacini
  • 65,180
  • 12
  • 72
  • 108