窗户CMD:问题FOR / F与引用命令参数报价窗户CMD:问题FOR / F与引用命令参数报价(w

2019-05-12 09:57发布

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

我已经无法使其工作,既不scaping引号,加倍他们,用反斜杠前面,改为单引号反引号和设置在相应的选项for命令,所有的组合,我试图失败。

如果要运行的命令被引用,它需要引用的论据失败。

是的,我知道surounding的引号find没有必要的,如果去掉,任何先前的四大行就可以了(忽略输出,delims,代币)

但在真正需要surounding命令引号的情况下(我知道系统中8dot3name行为被禁用),有没有什么办法,使工作? 我缺少的是在这里吗?

Answer 1:

这里有两种解决方案。

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 /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.


Answer 2:

这是比较容易将第一个令牌从for循环的指令后,令牌不带引号。

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


文章来源: windows cmd: problems with for /f with a quoted command with quoted parameters