Windows x64 和 Windows “路径中的括号”批处理文件问题

发布于 2024-10-24 19:19:48 字数 262 浏览 4 评论 0原文

Windows x64 版本包含以括号命名的文件夹,如“\Program Files (x86)”,这会破坏我使用的批处理文件。问题行的示例:

对于 (%path%) 中的 %%c,如果存在“%%c\xyz.exe”,请设置 xyz=OK

,即当它到达“(x86)”中的“)”时,它会输出一个错误消息并退出...

有关如何解决此问题的任何想法吗? 这是一个相当大的批处理文件,并且 atm 我没有时间用更好的语言重写它......

非常感谢:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送你一个梦 2024-10-31 19:19:48

不直接回答您的问题,但如果您尝试执行我认为您正在尝试的操作(即确保路径中存在文件),您可以在批处理文件中使用类似以下内容的内容。

   @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

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
弱骨蛰伏 2024-10-31 19:19:48

通常引用应该有效,但在这种情况下,您想要迭代由 ; 分隔的所有元素。

但是您可以将 ; 替换为 " " 组合,这样括号就会被引用,并且您可以迭代元素。

示例:path=C:\temp;C:\windows;C:\Program Files (x86)
for 循环将搜索
"C:\temp" "C:\windows" "C:\Program Files (x86)"

作为代码,它看起来像

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

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
)
撩起发的微风 2024-10-31 19:19:48

为此,您可以使用文件夹的短名称。这就是你的做法。

在 Windows 中打开命令提示符。
转到 C 驱动器(或包含程序文件夹的驱动器)
键入以下内容,

   c:\> dir /x  <Hit Enter>

这将返回所有文件夹的缩写形式。

您现在会注意到“\Program Files (x86)”将表示为“PROGRA~2”(或等效的短名称)。
这是我在创建批处理脚本时用来防止出现任何错误的方法。

有关更多选项,请参见此处。
http://www.computerhope.com/dirhlp.htm

“dir /”的解释x”
“这显示为非 8dot3 文件名生成的短名称。格式为 /N,在长名称之前插入短名称。如果不存在短名称,则在其位置显示空白。”

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文