1.

Solve : Problems with braces in batch files?

Answer»

Hi all,

I'm having a hard time with some () in a batch file. The problem is the following:

"test.bat" looks like this:

------------------------------------------------------------------------------------
@ECHO off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

call test2.bat "C:\Temp\Batch Test (bat)\test\test.txt"
------------------------------------------------------------------------------------

all it does is calling test2.bat which does the following:

------------------------------------------------------------------------------------
@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

set arg=%~1

if "!arg:~0,1!" == "/" (
echo ##### passed argument starts with a "/"
) else if "!arg:~-1!" == "\" (
echo ##### arg ends with a "\"
) else if exist "%~1" (
set ext=%~x1
if /i "!ext!" == ".txt" (
echo ##### is a text file
) else (
pushd %~dp1
dir
popd
)
) else (
echo ##### ERROR: %~1 not supported
)
------------------------------------------------------------------------------------


when I execute test.bat i get the following error: "\test\ was unexpected at this time."

So far the problem seems to be first if statement ( if "!arg:~0,1!" == "/" ( ).

In order to fix this I tried to remove the braces as soon as test2.bat gets the argument

set arg=!arg:^(=!
set arg=!arg:^)=!
set arg=!arg: =!

Unfortunately this does not help at all.

What am I doing WRONG? How can I make this work? Unfortunately the argument is GIVEN so I have to change the code, not the argument.

Thanks a lot

ACKH

Searching alt.msdos.batch.nt suggests failure to handle parentheses in file & folder names correctly is a BUG in NT command interpreter.

These: () are "parentheses"
these: [] are "BRACKETS"
these: {} are "braces"


Alright, thanks for your answer and for the technical terms. Seems that I have to find another way to get this done.

Regards,

ACKHUntil yesterday I thought they were braces also! It seems that a folder name that contains parentheses is a bad idea for batch processing and leads to much confusion. You say that renaming the folder is not an option?

I also had a problem running a batch script that had any kind of parentheses it is.
Have you tried using the DOS version of the folder?
DIR /X will give you the simple version to try.Thanks for your answers. It seems to me that parentheses are a problem whenever delayed environment variable expansion is used. Whenever I use it (for example "set TEST=!TEST! %%w" in a for loop) problems occur. What I did now was rewrite the script without delayed expansion which finally works.

Thanks and regards,

ACKHAppendix:
Whenever I call a batch file that has parentheses in its full file name (e.g. call "c:\test\hello (test)\example.bat") with arguments (e.g. call "c:\test\hello (test)\example.bat" /test D:\test) from another batch file i run into problems. This really seems to be a bug (as mentioned above).



Discussion

No Comment Found