2012-01-18 132 views
0

我需要迭代文件中的行。以下命令不起作用:轉義「)」在Windows批處理「的命令

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

因爲「)」在文件名中。錯誤:

\somewhere) was unexpected at this time. 

由「^」轉義不起作用,因爲我需要使用變量而不是內聯文件名。如何解決這個問題?

回答

4

將在雙引號中的文件名,而且還添加了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. 
+0

你必須擊敗我像3秒呃大聲笑 – Mechaflash 2012-01-18 17:17:41

+0

@Mechaflash:你的回答和我的一樣正確,所以+1不管。 :) – Jon 2012-01-18 17:20:26

+0

@Mechaflash:謝謝你們倆! – 2012-01-18 21:36:16

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

有usebackq允許您使用的路徑雙引號與空間