Batch file - IF syntax error

When I run this batch file from the command line, I get this error:

Command syntax is invalid.

This is done with my operator IF, but as far as I can tell, my syntax is correct - including ensuring that I have the right space around the brackets:

SET foo=bar

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

  ECHO "equal to"

) ELSE (
  :: bar

  ECHO "not equal to"
)
+4
source share
2 answers

The problem is using note tags (: :).

:: can only be used safely for notes outside blocks. Inside the blocks, he will try to execute the next line, since he assumes that a command follows the label.

An empty line after a note mark causes an error.

, . , echo - .

:

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

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

)

:

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

  ECHO "equal to"

) ELSE (
  rem bar

  ECHO "not equal to"
)

http://ss64.com/nt/rem.html

+3

:

SET foo=bar

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

  ECHO "equal to"

) ELSE (
  rem bar

  ECHO "not equal to"
)

:

SET foo=bar

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

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

cmd.exe. ( ::, ) . ,

+3

Source: https://habr.com/ru/post/1667426/


All Articles