Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

Any ideas on how to fix this? This is a rather large batch file, and atm I don't have the time to rewrite it in a better language...

Many thanks :)

like image 319
stereochilly Avatar asked Mar 21 '11 18:03

stereochilly


3 Answers

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
like image 149
sgmoore Avatar answered Sep 28 '22 15:09

sgmoore


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
)
like image 27
jeb Avatar answered Sep 28 '22 15:09

jeb


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."

like image 23
kensen john Avatar answered Sep 28 '22 15:09

kensen john