Andriy Tylychko
Andriy Tylychko

Reputation: 16286

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?

Upvotes: 0

Views: 661

Answers (2)

Anthony Miller
Anthony Miller

Reputation: 15930

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

Upvotes: 4

Jon
Jon

Reputation: 437904

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.

Upvotes: 4

Related Questions