批次檔call - windows cmd:用帶引號的參數的帶引號的命令for/f的問題
dos符號 (2)
for /f "delims=" %%a in ('"%systemRoot%\system32\find.exe" /?') do @echo %%a
是的,上一行工作。 沒有太多有用的,但工程。 但是嘗試寫一個批處理文件來回答另一個問題,我面對類似的東西
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
我一直無法做到這一點,既沒有用引號括起來,也沒有用雙引號括起來,在反斜杠前面,改成單引號來反引號,並在command中設置了相應的選項,我試過的所有組合都失敗了。
如果要運行的命令被引用,並且引用了參數,則失敗。
是的,我知道這個find
是不需要的,如果刪除了,前面四行中的任何一行都可以工作(忽略輸出,分隔符,令牌)
但是在真正需要引用命令的情況下(我知道8dot3name
行為被禁用的系統),是否有任何方法使其工作? 我在這裡錯過了什麼?
這裡有兩個解決方案。
1)有周圍的雙引號,並刪除^轉義字符。
2)在路徑上使用find。
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
這與在for命令中啟動額外的cmd進程來運行命令行有關。
奇怪的是,這三個命令在一個更簡單的上下文中失敗了。
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 \ notepad”“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.
將第一個令牌從for循環的指令放入一個不帶引號的令牌中比較容易。
for /f "delims=" %%a in (
' if defined OS "C:\my folder with spaces\consoleapp.exe" /param1:"value" '
)do @echo %%a