简体   繁体   English

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

[英]escape “)” in windows batch “for” command

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? 如何解决呢?

Put the filename in double quotes, but also add the usebackq option: 将文件名放在双引号中,还要添加usebackq选项:

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

From the output of FOR /? 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.
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 USEBACKQ允许您对带空格的路径使用双引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM