转义“)”在Windows批处理中“for”命令

发布于 2024-12-27 17:00:59 字数 295 浏览 0 评论 0原文

我需要迭代文件中的行。以下命令不起作用:

set filename=c:\program files (x86)\somewhere
...
for /f "delims==" %%i in (%filename%) do echo %%i

因为文件名中存在“)”。错误:

\somewhere) was unexpected at this time.

通过“^”转义在这里不起作用,因为我需要使用变量而不是内联文件名。如何解决这个问题?

I need to iterate lines in a file. Following command doesn't work:

set filename=c:\program files (x86)\somewhere
...
for /f "delims==" %%i in (%filename%) do echo %%i

because of ")" in the filename. Error:

\somewhere) was unexpected at this time.

Escaping by "^" doesn't work here because I need to use an variable instead of inline filename. How to resolve this?

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

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

发布评论

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

评论(2

拒绝两难 2025-01-03 17:00:59

将文件名放在双引号中,同时添加 usebackq 选项:

set filename=c:\program files (x86)\somewhere
for /f "usebackq delims==" %%i in ("%filename%") do echo %%i

FOR /? 的输出中:

usebackq        - specifies that the new semantics are in force,
                  where a back quoted string is executed as a
                  command and a single quoted string is a
                  literal string command and allows the use of
                  double quotes to quote file names in
                  file-set.

Put the filename in double quotes, but also add the usebackq option:

set filename=c:\program files (x86)\somewhere
for /f "usebackq delims==" %%i in ("%filename%") do echo %%i

From the output of FOR /?:

usebackq        - specifies that the new semantics are in force,
                  where a back quoted string is executed as a
                  command and a single quoted string is a
                  literal string command and allows the use of
                  double quotes to quote file names in
                  file-set.
少女的英雄梦 2025-01-03 17:00:59
set filename=c:\program files (x86)\somewhere
...
for /f "USEBACKQ delims==" %%i in ("%filename%") do echo %%i

USEBACKQ 允许您对带有空格的路径使用双引号

set filename=c:\program files (x86)\somewhere
...
for /f "USEBACKQ delims==" %%i in ("%filename%") do echo %%i

USEBACKQ allows you to use double quotes for paths with spaces

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