Gullydwarf
Gullydwarf

Reputation: 355

Batch file problems with IF statements and parantheses

I am trying to batch start some experiments. I need to redo some of them but not all. The ones where (s==1000 and r==0.5 and (c==1 or c==25 or c==75)) have been done ok so can be skipped (they take a long time to run). Ever since I introduced the IF statements, the way the parentheses are parsed is giving errors. I keep getting unexpected ( or ). I've tried rewriting the whole thing, but to no avail.

I know this is a lot like a 'please check my code for bugs' question, but I am really at my wits end here.

SET dotest=1
FOR %%s IN (1000,2000) do (
    FOR %%r IN (0.5,0.75,1) do (
        FOR %%c IN (1,25,75,100) do (
            IF (%%s==1000) (
                IF (%%r==0.5) (
                    IF (%%c==1)
                        (SET dotest==0)
                    IF (%%c==25)
                        (SET dotest==0)
                    IF (%%c==75)
                        (SET dotest==0)
                )
            )
            IF (%%dotest==1) (
                ECHO "Hello World!"
            )
        )
    )
)

Upvotes: 0

Views: 45

Answers (3)

MC ND
MC ND

Reputation: 70923

You can give it a try

     for %%s in ( 1000, 2000
) do for %%r in ( 0.5,  0.75,  1
) do for %%c in ( 1, 25, 75, 100
) do (
    set "doTest=1"
    if %%s==1000 if %%r==0.5 if not %%c==100 set "doTest="
    if defined doTest (
        echo "Hello World!"
    )
)

All the unneeded parenthesis and checks removed from code, while keeping the logic exposed both in question and original code.

In this case, as the variable is only used as a switch, instead of testing the value inside (that could need delayed expansion), if it tested if the variable is defined (has value) or undefined (has no value)

edit for a more usual code format

for %%s in (1000, 2000) do for %%r in (0.5, 0.75, 1) do for %%c in (1, 25, 75, 100) do (
    set "doTest=1"
    IF %%s==1000 if %%r=0.5 if not %%c==100 set "doTest="
    IF defined doTest (
        echo "Hello World!"
    )
)

Upvotes: 1

npocmaka
npocmaka

Reputation: 57252

@echo off

setlocal enableDelayedExpansion
FOR %%s IN (1000,2000) do (
    FOR %%r IN (0.5,0.75,1) do (
        FOR %%c IN (1,25,75,100) do (
            SET dotest=1
            IF %%s == 1000 (
                IF %%r == 0.5  (
                    IF %%c == 1 (
                       SET dotest=0
                    )
                    IF %%c==25 (
                        SET dotest=0
                    )
                    IF %%c==75 (
                        SET dotest=0
                    )
                )
            )
            IF !dotest! == 1 (
                ECHO "Hello World^!" %%s,%%r,%%c
            )
        )
    )
)

You should be able to run this without syntax errors - but there's no way for me to know if the logic is correct.The issues with your example:

  1. IF conditions are defined without brackets
  2. Start the brackets context after the if on the same line
  3. If you change the dotest value within brackets and need it again in this brackets content you'll need delayed expansion and ! instead of %
  4. In a SET command use only one equal sign

Upvotes: 3

MichaelS
MichaelS

Reputation: 6032

The syntax of IF is IF x==y (...) and not IF (x==y) (...). No () around the condition! Further you will have to add Setlocal EnableDelayedExpansion at the beginning of your script and address to dotest as !dotest! instead of %dotest%.

Upvotes: 2

Related Questions