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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

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

#1 Post by BoQsc » 02 Nov 2020 02:36

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.

npocmaka_
Posts: 512
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

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

#2 Post by npocmaka_ » 02 Nov 2020 04:11

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.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

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

#3 Post by jeb » 02 Nov 2020 04:27

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.

Post Reply