2014-03-25 18 views
5
for /f "delims=" %%a in ('"%systemRoot%\system32\find.exe" /?') do @echo %%a 

是的,上一行工作正常。沒有太大的作用,但有效。但是,試圖寫一個批處理文件來回答另一個問題,我面對像windows cmd:帶有帶引用參數的帶引號的命令的for/f問題

for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" ^< "c:\something.txt"') do @echo %%a 
for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" "c:\something.txt"') do @echo %%a 

無論是以前行返回The filename, directory name, or volume label syntax is incorrect

for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" ^< c:\something.txt' ) do @echo %%a 
for /f %%a in ('"%systemRoot%\system32\find.exe" /c /v "" c:\something.txt' ) do @echo %%a 

無論是以前行返回The system cannot find the file specified

我已經無法使其工作,既沒有引用報價,也沒有將它們加倍,在反斜槓前改爲單引號以反引號,並在for命令中設置相應選項,所有combinati我嘗試失敗。

如果要運行的命令被引用並且需要帶引號的參數,則失敗。

是的,我知道引號surounding不需要的find,如果刪除,任何先前的四大行就可以了(忽略輸出,delims,代幣)

但在情況下報價完全需要圍繞命令(並且我知道8dot3name行爲被禁用的系統),有什麼方法可以使其工作?我在這裏錯過了什麼?

回答

7

這裏有兩個解決方案。

1)有周圍的雙引號,並刪除^轉義字符。
2)使用查找,因爲它是在路徑上。

for /f %%a in ('""%systemRoot%\system32\find.exe" /c /v "" < "c:\something.txt""') do @echo %%a 

for /f %%a in (' find.exe /c /v "" ^< "c:\something.txt"') do @echo %%a 

這與啓動額外的cmd進程以在for命令內運行命令行有關。

奇怪的是,這三個命令在更簡單的上下文中失敗了。

for /f %%a in (' "c:\windows\system32\find.exe" /c /v "" something.txt ') do @echo %%a 

系統找不到指定的路徑。

for /f %%a in (' "c:\windows\system32\findstr.exe" /n "." something.txt ') do @echo %%a 

目錄名稱無效。

for /f %%a in (' "c:\windows\notepad" "something.txt" ') do @echo %%a 

「C:\ WINDOWS \記事本」,「something.txt」不被識別爲一個內部或外部的命令,可操作的程序或批處理文件。

這最後一個提供了外部引號被剝離的線索。

的Windows 8.1 32位

我想在這裏說明在cmd /?報價問題,當調用一個子進程:

If /C or /K is specified, then the remainder of the command line after 
the switch is processed as a command line, where the following logic is 
used to process quote (") characters: 

    1. If all of the following conditions are met, then quote characters 
     on the command line are preserved: 

     - no /S switch 
     - exactly two quote characters 
     - no special characters between the two quote characters, 
      where special is one of: &<>()@^| 
     - there are one or more whitespace characters between the 
      two quote characters 
     - the string between the two quote characters is the name 
      of an executable file. 

    2. Otherwise, old behavior is to see if the first character is 
     a quote character and if so, strip the leading character and 
     remove the last quote character on the command line, preserving 
     any text after the last quote character. 
+2

+1當然!!我忘了!該命令在新的cmd實例內運行。謝謝。我無法看到這一點。在這種情況下,第二個選項(路徑)被丟棄,因爲原始問題似乎與干擾執行的非本地'find'命令有關。 –

+2

爲了避免擾亂命令中的引用行爲:'in('^「」some cmd.exe「」some | arg「^」')''這可能是一個好主意。另外,我不記得任何細節,但我相信MS文檔中有一個小小的缺陷 - 我不認爲它完全描述了報價行爲。 – dbenham

0

這是比較容易把第一令牌從指令for循環到不帶引號的令牌中。

for /f "delims=" %%a in (
' if defined OS "C:\my folder with spaces\consoleapp.exe" /param1:"value" ' 
)do @echo %%a