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

windows cmd:for/f与带引号的参数的引号命令有关的问题,windows,batch-file,cmd,Windows,Batch File,Cmd,是的,前一行可以。没什么用处,但很管用。但试图编写一个批处理文件来回答另一个问题时,我遇到了如下问题 for /f "delims=" %%a in ('"%systemRoot%\system32\find.exe" /?') do @echo %%a 前面两行都返回系统找不到指定的文件 我无法让它工作,也没有跳过引号,将它们加倍,前面加反斜杠,将单引号改为反引号,并在命令中设置相应的选项,我尝试的所有组合都失败了 如果要运行的命令被引用,并且它使用被引用的参数,那么它将失败 是的,我知道围

是的,前一行可以。没什么用处,但很管用。但试图编写一个批处理文件来回答另一个问题时,我遇到了如下问题

for /f "delims=" %%a in ('"%systemRoot%\system32\find.exe" /?') do @echo %%a
前面两行都返回
系统找不到指定的文件

我无法让它工作,也没有跳过引号,将它们加倍,前面加反斜杠,将单引号改为反引号,并在
命令中设置相应的选项,我尝试的所有组合都失败了

如果要运行的命令被引用,并且它使用被引用的参数,那么它将失败

是的,我知道围绕
查找的引号是不需要的,如果删除,前面四行中的任何一行都会起作用(忽略输出、delims、token)


但是,如果确实需要围绕命令的引号(我知道禁用了
8dot3name
行为的系统),有什么方法可以让它工作吗?我在这里遗漏了什么?

这里有两个解决方案

1) 具有周围的双引号并删除了^escape字符。
2) 在路径上使用“查找”

 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
系统找不到指定的路径

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
“c:\windows\notepad”“something.txt”无法识别为内部或外部命令、可操作程序或批处理文件

最后一条提示外部引号正在被删除。

Windows 8.1 32位

我认为在调用子进程时,
cmd/?
中描述了报价问题:

for /f %%a in (' "c:\windows\notepad" "something.txt" ') do @echo %%a
如果指定了/C或/K,则在
开关作为命令行处理,其中包含以下逻辑
用于处理引号(“)字符:
1.如果满足以下所有条件,则引用字符
在命令行上,将保留:
-no/S开关
-正好两个引号字符
-两个引号字符之间没有特殊字符,
其中,special是以下之一:&()@^|
-字符之间有一个或多个空白字符
双引号字符
-两个引号字符之间的字符串是名称
一个可执行文件的名称。
2.否则,旧行为是查看第一个字符是否为
一个引号字符,如果是的话,去掉前导字符并
删除命令行上的最后一个引号字符,保留
最后一个引号字符后的任何文本。

将for循环指令中的第一个标记放入不带引号的标记中会更容易一些

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.

+1当然!!我忘了!该命令正在一个新的cmd实例中运行。谢谢。我无法看到这个。在本例中,第二个选项(路径)已放弃,因为原始问题似乎与干扰执行的非本机
find
命令有关。最好避开额外的引号,以免打乱命令中的引号行为:
in(“^”“some cmd.exe”“some | arg”^”)
。此外,我记不起任何细节,但我相信MS文档中有一个轻微的缺陷——我认为它不能准确描述报价行为。
for /f %%a in (' "c:\windows\notepad" "something.txt" ') do @echo %%a
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 /f "delims=" %%a in (
' if defined OS "C:\my folder with spaces\consoleapp.exe" /param1:"value" '
)do @echo %%a