- Any way to escape parenthesis characters in a batch file?
- Posted by WB on March 27th, 2008
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?
--
Bill Baker
- Posted by Big Al on March 28th, 2008
WB wrote:
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.
- Posted by Colin Barnhorst on March 28th, 2008
Can you use the short file name for Program Files (x86)? It is "PROGRA~2".
Or is that your workaround?
"WB" <WB@discussions.microsoft.com> wrote in message
news:C713DAEC-73F3-42FB-ABE5-6EFB594BC3E1@microsoft.com...
- Posted by Big Al on March 28th, 2008
WB wrote:
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.
- Posted by Pegasus \(MVP\) on March 28th, 2008
"Big Al" <BigAl@nowhere.com> wrote in message
news:JLWGj.9189$Oj5.1280@trnddc06...
They actually do. Try this for fun:
@echo off
if %UserName%==Al (echo Hello Al) else (echo Goodby %Username%)
- Posted by Pegasus \(MVP\) on March 28th, 2008
"WB" <WB@discussions.microsoft.com> wrote in message
news:C713DAEC-73F3-42FB-ABE5-6EFB594BC3E1@microsoft.com...
Try this:
@echo off
if "%PROCESSOR_ARCHITECTURE%"=="x86" (
set PROGNAME=Program Files
) else (
if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set PROGNAME=Program Files (x86)
)
- Posted by WB on March 28th, 2008
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
.
--
Bill Baker
"Pegasus (MVP)" wrote:
- Posted by Colin Barnhorst on March 28th, 2008
You never did comment on whether "progra~2" could work for you. Can't you
use the short filename?
"WB" <WB@discussions.microsoft.com> wrote in message
news:C49F7B14-DDF4-4F6E-9BF6-5153E3F81854@microsoft.com...
- Posted by Pegasus \(MVP\) on March 28th, 2008
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.
"WB" <WB@discussions.microsoft.com> wrote in message
news:C49F7B14-DDF4-4F6E-9BF6-5153E3F81854@microsoft.com...
- Posted by WB on March 28th, 2008
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.
--
Bill Baker
"Colin Barnhorst" wrote:
- Posted by Colin Barnhorst on March 28th, 2008
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.
"WB" <WB@discussions.microsoft.com> wrote in message
news:3C38122B-B84B-484F-B3BB-78B7041BFC08@microsoft.com...
- Posted by Big Al on March 28th, 2008
Pegasus (MVP) wrote:
- Posted by Erwan on April 4th, 2008
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
"WB" wrote: