Page 1 of 1

Nesting statement if ()==() ( ) gives an error

Posted: 02 Nov 2020 02:36
by BoQsc
These IF statements are suppose to compare strings.
As you will see we are comparing: literal "c" character with another literal "c" character.
And later on, adding another if statement that compare: literal "b" character with another literal "b" character.
Or at least I think so.

First test, everything is ok

Code: Select all

@ECHO OFF
IF (c)==(c) (
	echo test
)
Output:

Code: Select all

test

Second test, there is an Error

Code: Select all

@ECHO OFF
IF (c)==(c) (
	echo test
	IF (b)==(b) (
		echo yes
	(
)
Output:

Code: Select all

) was unexpected at this time.

Re: Nesting statement if ()==() ( ) gives an error

Posted: 02 Nov 2020 04:11
by npocmaka_
One of your closing brackets is actually an opening one.

Try like this:

Code: Select all

@ECHO OFF
IF (c)==(c) (
	echo test
	IF "(b)"=="(b)" (
		echo yes
	)
)
the closing bracket in the nested if compared strings is taken for a closing bracket of the outer if.

May be this example will be more descriptive:

Code: Select all

IF (c)==(c) ( IF (b^)==(b^) ( echo yes ) )
- this will work.

Re: Nesting statement if ()==() ( ) gives an error

Posted: 02 Nov 2020 04:27
by jeb
Hi BoQsc,

avoid enclosing anything with parenthesis in the IF's, at all.
Enclosing should always be done with quotes.

Code: Select all

IF "c" == "c" (
	echo test
	IF "b" == "b" (
		echo yes
	)
)
Using quotes, avoids problems with spaces in the value (in case of variables) and also special characters cna be used.
Parenthesis are complete useless in `IF` statements and they only adds undesired behaviour.