4

When I run this batch file from a command-prompt, I get this error:

The syntax of the command is incorrect.

It's to do with my IF statement, but as far as I can tell my syntax is correct - including ensuring I have the necessary whitespace around the parenthesis:

SET foo=bar

:: test
IF "%foo%" EQU "bar" (
  :: foo

  ECHO "equal to"

) ELSE (
  :: bar

  ECHO "not equal to"
)
1

2 Answers 2

3

The problem is your use of labels for remarks (::).

:: can only be safely used for remarks outside of blocks. Inside blocks it'll try to execute the next line as it assumes the label is followed by a command.

The blank line after the label-remark causes the error.

You can remove the blank line to remove the error. In this case the parser assumes the label and the echo command are the command to execute.

Try this:

SET foo=bar
:: test
IF "%foo%" EQU "bar" (
  :: foo
  ECHO "equal to"

) ELSE (
  :: bar
  ECHO "not equal to"

)

A safer option is to use the real remark command:

SET foo=bar
rem test
IF "%foo%" EQU "bar" (
  rem foo

  ECHO "equal to"

) ELSE (
  rem bar

  ECHO "not equal to"
)

See more at http://ss64.com/nt/rem.html

Sign up to request clarification or add additional context in comments.

3 Comments

I will eviscerate the author of the Batch file grammar with a rusty spork, I swear!
Give the guy some credit. The original command.com was created in assembly running under tight RAM constraints and batch support was probably added as an afterthought. The question is why do you continue to torture yourself writing batch files in this day and age :)
Windows Azure cloud service startup scripts :/
3

try like this:

SET foo=bar

:: test
IF "%foo%" EQU "bar" (
  rem foo

  ECHO "equal to"

) ELSE (
  rem bar

  ECHO "not equal to"
)

or like this:

SET foo=bar

:: test
IF "%foo%" EQU "bar" (
  ::foo
  ECHO "equal to"

) ELSE (
  ::bar
  ECHO "not equal to"
)

this is a bug in the cmd.exe. Labels (in this case :: treated like a label ) does not work good in brackets context. After a label a command is expected but not an empty line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.