开发者

Windows x64 & "parenthesis in path" batch file problem

开发者 https://www.devze.com 2023-02-18 00:48 出处:网络
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:

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 r开发者_Python百科eaches ")" 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 :)


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


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
)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消