windows cmd:for/f 的问题,带有带引号的参数的带引号的命令 [英] windows cmd: problems with for /f with a quoted command with quoted parameters

查看:30
本文介绍了windows cmd:for/f 的问题,带有带引号的参数的带引号的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

是的,上一行有效.没有多大用处,但有效.但是尝试编写一个批处理文件来回答另一个问题,我遇到了类似

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%system32find.exe" /c /v "" ^< "c:something.txt"') do @echo %%a
 for /f %%a in ('"%systemRoot%system32find.exe" /c /v ""    "c:something.txt"') do @echo %%a

前两行返回文件名、目录名或卷标语法不正确

 for /f %%a in ('"%systemRoot%system32find.exe" /c /v "" ^< c:something.txt'  ) do @echo %%a
 for /f %%a in ('"%systemRoot%system32find.exe" /c /v ""    c:something.txt'  ) do @echo %%a

前两行都返回系统找不到指定的文件

我一直无法使它工作,既没有转义引号,也没有将它们加倍,在前面加上反斜杠,将单引号改为反引号并在 for 命令中设置相应的选项,所有组合我尝试失败了.

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.

是的,我知道不需要 find 周围的引号,如果删除,前四行中的任何一行都可以工作(忽略输出、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)

但是在真正需要命令周围的引号的情况下(我知道禁用 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?

推荐答案

这里有两个解决方案.

1) 包含双引号并删除了 ^ 转义字符.
2) 在路径上使用 find .

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

for /f %%a in ('""%systemRoot%system32find.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 命令内的命令行有关.

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:windowssystem32find.exe" /c /v ""  something.txt ') do @echo %%a

系统找不到指定的路径.

The system cannot find the path specified.

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

目录名无效.

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

'c:windows otepad" "something.txt' 不是内部或外部命令,也不是可运行的程序或批处理文件.

'c:windows otepad" "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.

这篇关于windows cmd:for/f 的问题,带有带引号的参数的带引号的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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