Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

brakets in variables content destroy batch execution

30 views
Skip to first unread message

Arno

unread,
Sep 3, 2010, 12:39:48 PM9/3/10
to
Hi all,

we made a lot of batch scripting in our projects. Since we got more
and more 64 bit operating systems we got trouble, because of brakets
in some default pathnames, like 'C:\Program Files (x86)' for examples.
This varibles break all the batch code, what are written in some
structured way with 'if ... ( ... ) else ( ... )'.
Is there a way to prevent this errors, with some kind of quoting or
something else?

The problem is that you didn't know at any point, if you make a call
to somewhat, what is the content of a given parameter or variable.

any help or hint would be appreciated
thx

Arno

foxidrive

unread,
Sep 3, 2010, 12:54:55 PM9/3/10
to

The solution often depends on the code you are using.

--
Regards,
Mic

Frank P. Westlake

unread,
Sep 4, 2010, 9:16:43 AM9/4/10
to
"Arno"
news:b21174aa-e70d-4309...@u31g2000pru.googlegroups.com...

> ... like 'C:\Program Files (x86)' for examples.


> This varibles break all the batch code, what are written in some
> structured way with 'if ... ( ... ) else ( ... )'.

In many cases the solution is to escape all ')' characters as '^)'. For
example:

'C:\Program Files (x86^)'

If the string is being read into a variable you can use string
substitution:

REM var is read from a file as "C:\Program Files (x86)"

Set "var=%var:)=^)%"

REM var is now "C:\Program Files (x86^)"

Frank


Arno

unread,
Sep 6, 2010, 3:22:40 AM9/6/10
to
>   Set "var=%var:)=^)%"
>

Do I have also to quote the opened brackets?

I want to have some general way to make some kind
of substitutions, because I have a lot of situautions
where I use the PATH variable to make some checks or
anything else.
So now it is realy a pain, because sometimes onyl a
echo of such a variable destroy the coding.

Arno

jeb

unread,
Sep 6, 2010, 5:08:03 AM9/6/10
to

Hi,

It seems to me, that nobody understand how and why characters are
escaped or how they are expanded.

I made some (many) experiments, and this are my results:

A line of code in a batch file has multiple phases (on the command
line the expansion is different!).
The process starts always with phase 1

0. Phase(call/caret doubling): Only if the line is reprocessed by a
call
- Double all carets (the normal carets seems to be stay unchanged,
because in phase 2 they are reduced to one, but in quotes they are
effectivly doubled)

1. Phase(Percent):
- Expansion of %var%, if var does not exists replace it with nothing
- A double %% is replaced by a single %

2. Phase(Special chars): Look at each character
- If it is a quote (") toggle the quote flag, if the quote flag is
active, the other special characters are ignored (^&|<><bracket>)
- If it is a caret (^) the next character has no special meaning, the
caret itself is removed, if the caret is the last character of the
line, the next line is appended, the first charater of the next line
is always handled as escaped charater.
- If it is on of the special characters (^&|<><bracket>) split the
line at this point, in case of the pipe (|) both parts gets a phase
restart (a bit more complex ...)
- In this phase the "Tokens" are build, token delimiters are
<space>,;=
- If the first token is "rem", only two tokens are processed,
important for the multiline caret

3. Phase(echo): If "echo is on" print the result of phase 1 and 2
- For-loop-blocks are echoed multiple times, first time in the context
of the for-loop, with unexpanded for-loop-vars
- For each interation, the block is echoed with expanded for-loop-vars

4. Phase(for-loop-vars expansion): Expansion of %%a and so on

5. Phase(Exclamation mark): Only if delayed expansion is on, look at
each character
- If it is a caret (^) the next character has no special meaning, the
caret itself is removed
- If it is an exclamation mark, search for the next exclamation mark
(carets are not observed anymore), expand to the content of the
variable

- If no exclamation mark is found in this phase, the result is
discarded, the result of phase 4 is used instead (important for the
carets)
- Important: At this phase quotes and other specical characters are
ignored
- Expanding vars at this stage is "safe", because special characters
are not detected anymore

6. Phase(call): If the first token is "call", start with phase 0
again, but stops after phase 2

7. Phase(Execute): The command is executed
- In case of a --->set "name=content"<---, the complete content of the
first equal sign to the last quote of the line is used as content-
token, if there is no quote after the equal sign, the rest of the line
is used.

Hope it helps
Jan Erik

Arno

unread,
Sep 6, 2010, 6:56:14 AM9/6/10
to
>
> 7. Phase(Execute): The command is executed
> - In case of a --->set "name=content"<---, the complete content of the
> first equal sign to the last quote of the line is used as content-
> token, if there is no quote after the equal sign, the rest of the line
> is used.
>
This helps a lot, specialy the last point with quoting the set
parameters
as a whole, help me a lot. With this, most problems with the brackets,
disapears.

Thanks a lot
Arno

Frank P. Westlake

unread,
Sep 6, 2010, 9:21:11 AM9/6/10
to
"Arno"
news:cb5a3573-1cd0-49aa...@s9g2000yqd.googlegroups.com...

> Do I have also to quote the opened brackets?


I don't recall having experienced a situation where it is necessary.
Some people do because it doesn't hurt, but I escape only the right
parenthesis.

I don't know if this has changed but it used to be:

() = parentheses
[] = brackets
{} = braces

Some people would say "square brackets" and "curley braces" to help
avoid confusion. When I worked in communications '(' was PAREN and ')'
was UNPAREN, so I guess BRACKET/UNBRACKET and BRACE/UNBRACE could also
be useful.

Frank


0 new messages