windows cmd:用带引号的参数的带引号的命令for / f的问题

for /f "delims=" %%a in ('"%systemRoot%\system32\find.exe" /?') do @echo %%a 

是的,上一行工作。 没有太多有用的,但工程。 但是尝试写一个batch file来回答另一个问题,我面对类似的东西

  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