窗户CMD:问题FOR / F具有引用的命令参数报价 [英] windows cmd: problems with for /f with a quoted command with quoted parameters

查看:223
本文介绍了窗户CMD:问题FOR / F具有引用的命令参数报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

是的,previous线工作。没有多少有用的,但作品。但是,试图写一个批处理文件来回答另一个问题,我面对类似

Yes, the previous line works. Not much useful but works. But trying write a batch file to answer another question, i faced something like

 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

无论是previous线返回文件名,目录名或卷标语法不正确

 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

无论是previous线返回系统找不到指定文件

我已经无法使其工作,既不scaping的报价,他们增加了一倍,$ P $与反斜杠pceding,更改为单引号反引号和设置在相应的选项命令,我试图失败所有组合。

I've been unable to make it work, neither scaping the quotes, doubling them, preceding with backslashes, changing to single quotes to backquotes and setting the corresponding option in for command, all the combinations that i tried failed.

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

If the command to run is quoted and it takes quoted arguments it fails.

是的,我知道引号surounding的找到是必须的,如果删除,任何previous四行将工作(忽略输出, delims,令牌)

And yes, i know the quotes surounding the find are not needed, and if removed, any of the previous four lines will work (ignoring the output, delims, tokens)

但在真正需要surounding命令引号的情况下(我知道系统中 8dot3name 行为被禁用),有没有办法让它工作吗?我失去了什么吗?

But in the case where the quotes surounding the command are really needed (and i know systems where 8dot3name behaviour is disabled), is there any way to make it work? what am i missing here?

推荐答案

下面是两种解决方案。

Here are two solutions.

1),周围有双引号并删除^转义字符。结果
2)使用发现,因为它是在路径上。

1) has surrounding double quotes and removed ^ escape character.
2) uses find as it is on the path.

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进程运行里面的命令行命令用于做。

It's to do with launching an extra cmd process to run the command-line inside the for command.

奇怪的是,这三个命令在一个更简单的情况下失败是不同的。

Curiously, these three commands fail differently in an even simpler context.

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

系统找不到指定的路径。

The system cannot find the path specified.

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

目录名称是无效的。

The directory name is invalid.

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

C:\\ WINDOWS \\记事本,something.txt'。不被识别为一个内部或外部命令,可操作的程序或批处理文件

'c:\windows\notepad" "something.txt' is not recognized as an internal or external command, operable program or batch file.

最后一项给出了一个线索外引号被剥夺。

Windows 8.1中的32位。

Windows 8.1 32 bit

我想在这里描述的报价问题 CMD / 当调用一个子进程:

I think the quote issue is described here in cmd /? when a child process is invoked:

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.

这篇关于窗户CMD:问题FOR / F具有引用的命令参数报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆