简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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