if NOT (...==....) does not work inside script?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

if NOT (...==....) does not work inside script?

#1 Post by pstein » 03 Aug 2013 23:30

I want to do somethig like the follwoing inside a DOS *.bat script in Win7:

if ....... (
.....
set foobar=
set /p parm=enter parameter
if NOT ([!parm!]==[]) (
set foobar="aabbccdd!parm!")
.....
)


Unfortunately when it comes to the "If" statement the batch script engine crashes and tells me

) was unexpected at this time.

How else can I code it?

Be aware: The following (=leaving inner brackets) does NOT represent what I want:

if ....... (
.....
set foobar=
set /p parm=enter parameter
if NOT [!parm!]==[] (
set foobar="aabbccdd!parm!")
.....
)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: if NOT (...==....) does not work inside script?

#2 Post by foxidrive » 04 Aug 2013 02:23

Try using quotes - they also protect against spaces and poison characters.

Code: Select all

if NOT "[!parm!]"=="[]"



Your issue is that the closing brackets need to be escaped in a loop.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: if NOT (...==....) does not work inside script?

#3 Post by penpen » 04 Aug 2013 05:19

Assumed that no closing parenthesis is outstanding, then another issue may be,
that you compare "(a" with "a)" (without the doublequotes), if you do something like this:
pstein wrote:if (a==a) echo a==a


Note:
Parenthesis are handled in a kind of weird way (this is only my opinion):
Some (not all) instructions handle an open parenthesis as a normal character that doesn't have to be escaped.(*1)
If no closing parenthesis is outstanding, then it is ignored, if it stands alone (*2).
But it is handled as a normal character, if it is within an instruction (*3).
If an closing parenthesis is outstanding, then it is handled as an end block (*4).


Code: Select all

:: (*1)
if (a == (a echo (a == (a

:: (*1)(*3)
echo Parenthesis ()
if (b==b) echo This is not displayed.



for %%a in (1 2 3 4) do (
   :: 1 example for (*1), (*4)
   if %%a==%%a echo (%%a==%%a)
echo Not in loop so displayed only once

:: (*2)
)



penpen

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: if NOT (...==....) does not work inside script?

#4 Post by pstein » 05 Aug 2013 04:19

foxidrive wrote:Try using quotes - they also protect against spaces and poison characters.

Code: Select all

if NOT "[!parm!]"=="[]"


Your issue is that the closing brackets need to be escaped in a loop.


Hmm, thank you but I am still confused. Ok, you said I should replace round brackets by double quotes.

But then my if clause should look like:

Code: Select all

if NOT "[!parm!]==[]"


and not like you suggested.

As I wrote before

NOT a==b

is NOT the same as

NOT (a==b)

even on the top if level.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: if NOT (...==....) does not work inside script?

#5 Post by penpen » 05 Aug 2013 06:19

Foxidrive never said you should replace the parenthesis with doublequotes:
You have to escape the closing brackets if you want to use them standalone (outside doublequotes).

Foxidrive's example additionally corrects your example by removing the parenthesis,
assumed that you don't want to use it, because this seems obvious.

Additionally you should read my post again, but i show you what you are doing in c++/Java syntax,
as i assume you have programmed something like this before; if you need it in another programming language just write it.
Not int32 example:

Code: Select all

::bat code
if NOT abc==ABC ...

// in C++/Java
if (!("abc".equals ("ABC"))) {
  ...
}


::bat code, assumed no outstanding ), otherwise you have to escape it
if NOT (abc==ABC) ...

:: escaped )
if NOT (abc==ABC^) ...


// in C++/Java
if (!("(abc".equals ("ABC)"))) {
  ...
}

An int32 example:

Code: Select all

::bat code
if NOT 123==456 ...

// in C++/Java
if (!(123 == 456)) {
  ...
}


::bat code, assumed no outstanding ), otherwise you have to escape it
if NOT (123==456) ...

:: escaped )
if NOT (123==456^) ...


// in C++/Java
if (!("(123".equals ("456)"))) {
  ...
}

penpen

Post Reply