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!")
.....
)
if NOT (...==....) does not work inside script?
Moderator: DosItHelp
Re: if NOT (...==....) does not work inside script?
Try using quotes - they also protect against spaces and poison characters.
Your issue is that the closing brackets need to be escaped in a loop.
Code: Select all
if NOT "[!parm!]"=="[]"
Your issue is that the closing brackets need to be escaped in a loop.
Re: if NOT (...==....) does not work inside script?
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:
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).
penpen
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
Re: if NOT (...==....) does not work inside script?
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.
Re: if NOT (...==....) does not work inside script?
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:
An int32 example:
penpen
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