stereochilly
stereochilly

Reputation: 123

Windows x64 & "parenthesis in path" batch file problem

Windows x64 versions contain folders named with parenthesis like \Program Files (x86) and this breaks a batch file I use. An example of a problem line:

for %%c in (%path%) do if exist "%%c\xyz.exe" set xyz=OK

i.e., when it reaches ) in (x86) it puts out an error message and exits...

How do I fix this?

Upvotes: 5

Views: 5748

Answers (3)

jeb
jeb

Reputation: 82390

Normally quoting should work, but in this case you want to iterate over all elements seperated by ;.

But you can replace the ; to a " " combination, so the brackets are quoted and you can iterate over the elements.

sample: path=C:\temp;C:\windows;C:\Program Files (x86)
The for-loop will search in
"C:\temp" "C:\windows" "C:\Program Files (x86)"

As code it looks like

setlocal EnableDelayedExpansion
set "searchPath=!path:;=" "!"
for %%c in ("!searchPath!") do (
    if exist "%%~c\xyz.exe" set xyz=OK
)

Upvotes: 3

sgmoore
sgmoore

Reputation: 16077

Doesn't directly answer your question, but if you are trying to do what I thinking you are trying (which is make sure a file exists in the path) you can use something like the following in a batch file.

   @echo off
   for %%i in (xyz.exe) do set xyz=%%~$PATH:i

   if "%xyz%" == "" Goto NotFound

   Echo "Found"
   Goto TheEnd

:NotFound
   Echo "Not found"

:TheEnd

Upvotes: 5

kensen john
kensen john

Reputation: 5519

You can use the short names of the folder for this purpose. This is how you do it.

Open command promt in Windows. Go to C drive (or the drive in which you have the Program Folder) Type the following and

   c:\> dir /x  <Hit Enter>

This will return the short forms of all folders.

You will notice now that "\Program Files (x86)" will be represented as "PROGRA~2" (or an equivalent short name). This is what I use to prevent any errors while creating Batch scripts.

For more options see here. http://www.computerhope.com/dirhlp.htm

Exlpanation for "dir /x"
"This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place."

Upvotes: 2

Related Questions