Any way to escape parenthesis characters in a batch file?

W

WB

if "%PROCESSOR_ARCHITECTURE%"=="x86" (
set PROGNAME=Program Files
) else if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
set PROGNAME=Program Files (x86)
)

This fails because of the parenthesis in (x86). I have a workaround already,
but I'd prefer using the method above for readability. Any way to escape the
parenthesis characters so this will work?
 
B

Big Al

WB said:
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
set PROGNAME=Program Files
) else if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
set PROGNAME=Program Files (x86)
)

This fails because of the parenthesis in (x86). I have a workaround already,
but I'd prefer using the method above for readability. Any way to escape the
parenthesis characters so this will work?
I didn't think .bat files would accept if/else commands.
I know 'if exists ... goto' works. I use it to skip to another line in
a batch file, and thus the next line is the else.
But not much else you can do.
 
C

Colin Barnhorst

Can you use the short file name for Program Files (x86)? It is "PROGRA~2".
Or is that your workaround?
 
B

Big Al

WB said:
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
set PROGNAME=Program Files
) else if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
set PROGNAME=Program Files (x86)
)

This fails because of the parenthesis in (x86). I have a workaround already,
but I'd prefer using the method above for readability. Any way to escape the
parenthesis characters so this will work?
see http://home7.inet.tele.dk/batfiles/batfiles.htm
I was partially right. Use the GOTO to get to another line of code

if ........... goto OKAY
rem here if IF is false
goto END

:OKAY
rem here if IF is true
goto END


:END
rem here on both.
 
P

Pegasus \(MVP\)

Big Al said:
I didn't think .bat files would accept if/else commands.
I know 'if exists ... goto' works. I use it to skip to another line in a
batch file, and thus the next line is the else.
But not much else you can do.

They actually do. Try this for fun:

@echo off
if %UserName%==Al (echo Hello Al) else (echo Goodby %Username%)
 
P

Pegasus \(MVP\)

WB said:
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
set PROGNAME=Program Files
) else if "%PROCESSOR_ARCHITECTURE%"=="AMD64" (
set PROGNAME=Program Files (x86)
)

This fails because of the parenthesis in (x86). I have a workaround
already,
but I'd prefer using the method above for readability. Any way to escape
the
parenthesis characters so this will work?

Try this:
@echo off
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
set PROGNAME=Program Files
) else (
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set PROGNAME=Program Files (x86)
)
 
W

WB

This is pretty much the same code I had. Unfortunately it sets PROGNAME as
"Program Files (x86" with the missing closed parenthesis.

My workaround is as follows:
SET PROGNAME=Program Files (x86)
IF "%PROCESSOR_ARCHITECTURE%"=="x86" SET PROGNAME=Program Files

This will also work if PROCESSOR_ARCHITECTURE changes to "AMD128", or
"Intel128" like it should be :).
 
C

Colin Barnhorst

You never did comment on whether "progra~2" could work for you. Can't you
use the short filename?
 
P

Pegasus \(MVP\)

My suggestion may have been pretty much the same as
your own code, with a slight difference: It was able to handle
if-then-else statements. Your work-around is, of course,
an excellent alternative solution.
 
W

WB

The short filename method may work as well, but I prefer my workaround method
just for clean-looking code. Plus I just have a deep hatred for short
filenames, and there's always that one-in-a-million chance that something
will happen to make it "progra~3". We had an issue recently where a DOS
program had to look for a text file. A backup of the file had been made, and
somehow the two short filenames got switched, causing the DOS app to fail.
 
C

Colin Barnhorst

I know of one program installer that does not recognize parentheses in a
pathname (Adobe CS2) and misinstalls on 64bit Windows. The short filename
is an exact equivalent of "Program Files (x86)" and has to be substituted in
the "what folder do you want to install to" dialog for CS2 to work in XP Pro
x64 or Vista x64 so I thought it might be the way to go for you. There is
no danger that progra~2 will ever become progra~3 by accident or design.
Short filenames should always work even when long filenames do not for some
reason or another.
 
E

Erwan

Bill,

You can escape characters in a batch file by using ^. It works for
parenthesis, pipe, ampersand, everything. In a related example, the following
lines failed for me:
IF %NUM_DEL_FILES% == 0 (
ECHO No files(s) deleted
)

However, I can get it to run by escaping the closing parenthesis as folows:
IF %NUM_DEL_FILES% == 0 (
ECHO No files(s^) deleted
)

Another (undocumented) use of ^ is to break a single command in several lines.

-- Erwan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top