FORFILES没有CMD / C [英] forfiles without cmd /c

查看:165
本文介绍了FORFILES没有CMD / C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以运行 FORFILES 命令
CMD / C ,符合市场预期。

I can run a forfiles command with cmd /c, as expected


C:\>forfiles /c "cmd /c ping /a"
IP address must be specified.

但是,如果我删除 CMD / C ,它不再认识任何参数,只
基本命令

However if I remove the cmd /c, it no longer recognizes any arguments, only the base command


C:\>forfiles /c "ping /a"
Usage: ping [-t] [-a] [-n count] [-l size] [-f] [-i TTL] [-v TOS]
            [-r count] [-s count] [[-j host-list] | [-k host-list]]
            [-w timeout] [-R] [-S srcaddr] [-4] [-6] target_name

我必须使用 CMD / C ,即使在PATH外部命令?

Must I use cmd /c, even with external commands on the PATH?

推荐答案

这是原来的答案。请阅读问题的完整的分析和一个更好的办法来解决它的线下

命令及其参数之间放置两个空格

Place two spaces between the command and its arguments

forfiles /c "ping  /a"


编辑虽然发布解决方案适用于OP的问题,它不是一个完整的答案,因为有一些情况下,这个选项是行不通的。


edited While the posted solution works for the OP question, it is not a complete answer, as there are some cases where this option does not work.

为什么?

我开始调试 FORFILES 参数分析例程想在这样的命令字符串被解析的问题,并转化为生成最终的运行命令。

I started to debug the forfiles argument parsing routines thinking the problem is in the way the command string is parsed and converted to generate the final running command.

没有,它工作没有任何问题。

No, it works without any problem

问题的原因是在调用的CreateProcess API函数和方法的C参数语法分析程序工作原理和程序员通常处理的参数。

The cause of the problem is in the call to the CreateProcess API function and the way the C argument parser works and how the programmers usually handle the arguments.

FORFILES 调用API为

CreateProcess('c:\windows\system32\ping.exe','/a', ....)

也就是说,应用程序名称和参数吧。尼斯和干净,但有问题的,因为第一个参数传递给应用程序 / A

问题出在哪里?在第一个参数。通常的争论焦点处理在几乎任何程序假定第一个参数的程序是程序本身(至少它的名字),也就是的argv [0] 是程序名称。

Where is the problem? In the first argument. The usual argument handling in almost any program assumes that the first argument to the program is the program itself (at least its name), that is, argv[0] is the program name.

但它的行为是那样,从 FORFILES 的CreateProcess 的调用应该是任何

But for it to behave that way, the call to CreateProcess from forfiles should be any of

CreateProcess('c:\windows\system32\ping.exe','ping.exe /a', ....)
CreateProcess(NULL, 'c:\windows\system32\ping.exe /a', .... )

在C作为几乎所有人编程(在很多遵循相同的约定更多的语言),期待该的argv [0] (参数值表第一的位置)会是程序名称,的argv [1] (参数值表,第二个位置)将是第一个参数,因为这是不是在的情况下FORFILES 启动的进程,第一个参数会被忽略,因为第一个真正的参数将被存储在的argv [0] ,而不是的argv [1]

As almost everyone programming in C (and in a lot more languages that follow the same convention) is expecting that argv[0] (arguments value table first position) will be the program name and argv[1] (arguments value table, second position) will be the first argument, and as this is not the case in forfiles started processes, the first argument will be ignored because the first real argument will be stored in argv[0], not argv[1]

所以,正确的行为或失败将取决于被调用的程序所使用的解析器/词法/分词器。

So, correct behaviour or failure will depend on the parser/lexer/tokenizer used by the called program.


  • 他们中有些人会看到增加的空间将被存储在 argv的一个aditional的参数[0] (标准标记生成器中的mingw / gcc和VC这样的行为)。

  • Some of them will see the added space as an aditional argument that will be stored inside argv[0] (the standard tokenizer in mingw/gcc and VC behave this way).

别人会删除空格,并采取第一个非空数据作为的argv [0] 的情况下,找到

Others will remove the spaces and take the first non blank data as argv[0] (the case of find)

你能想到的任何其他行为可以通过标记生成器通过。

Any other behaviour you can think can be adopted by the tokenizer.

而一旦标记生成器结束其工作,该方案将处理的参数,并选择之一

And once the tokenizer has ended its work, the program will handle the arguments and select one of


  • 忽略第一个参数,因为它是assummed程序名是在这个位置

  • Ignore the first argument as it is assummed the program name is in this position

请什么将在命令行中没有发现任何假设和确定的说法。

Make no assumptions on what will be found in the command line and identify the argument.

因此​​,空间的解决方案是不是一个防弹解决方案(谢谢dbenham)

So, the space solution is not a bulletproof solution (thank you dbenham)

如何解决呢?

由于这一问题是程序名在命令行参数ausence,并符合下列参数的位置不好,最好的选择似乎包括它(当然,我们可以包括任何被用来作为的argv [0] ,但由于大多数程序期望的程序名...)

As the problem is the ausence of the program name in the command line argument, and the bad location of the following arguments, the best option seems to include it (well we can include anything to be used as argv[0], but as most programs expect the program name ...)

forfiles /c "ping ping -a"
forfiles /c "find find /c /v 0x220x22 @path"

这篇关于FORFILES没有CMD / C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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