在 Windows 批处理文件中作为参数传递给 exe 时扩展 *.*
我正在 Windows XP 中编写一个批处理文件。我已将一组 *.ts
文件复制到我的 exe 目录中。 TS 文件的数量和名称一样不固定。
现在我想运行我的 exe 之一,它将所有 TS 名称作为参数。
在Linux中我尝试过像
这有效。但是当我在 Windows 中执行相同操作时,它不会扩展 *.ts
。
请告诉我如何在将参数传递给我的 exe 时扩展 *.ts
。
I am writing a batch file in Windows XP. I have copied a set of *.ts
files to the directory of my exe. The number of TS files are not fixed so as their names.
Now I want to run one of my exe which will take all the TS names as argument.
In Linux I have tried like<MyExeName> *.ts
This worked. But when I do the same in Windows it's not expanding the *.ts
.
Please let me know how I can expand the *.ts
while passing arguments to my exe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Windows shell(命令处理器)在调用外部命令时从不执行任何通配符操作;你必须自己做。对于 C,请参阅Windows 上 C++/C 中的通配符。
The Windows shell (command processor) never does any globbing when calling external commands; you have to do it yourself. For C, see Globbing in C++/C, on Windows.
您可以使用 FOR 循环来枚举所有 *.ts 文件,例如
You could use a FOR-Loop to enumarate all *.ts files, like
在
SET /?
输出的末尾附近是关于延迟环境变量提取的宝石。它展示了如何使用相对较新的符号(从 NT 3.1 开始?它适用于 XP 和 Win 7)来延迟扩展环境变量,以构建与单个变量中的通配符匹配的文件名列表。请注意,这里存在一个问题,即引用包含空格或其他“有趣”字符的名称,我将这些字符留给学生作为练习。在 CMD.EXE 中正确引用甚至比在任何 Unix shell 中正确引用还要困难。
当然,用您的命令行替换
echo
命令。编辑:据观察,这在批处理文件中似乎效果不佳,并且它取决于启用的延迟扩展的特定功能。
延迟扩展功能通过 CMD.EXE 的
/V:ON
开关启用,或者通过注册表项全局调用 CMD。详细信息记录在CMD /?
的输出中。转移到批处理文件后,您会遇到一些问题,并且可以轻松修复以启用该功能。关键是
SETLOCAL
命令有一个可以随意打开和关闭延迟功能的选项。来自CMD /?
:此外,在某些上下文中,例如
FOR
命令,还需要将百分号加倍。总之,我会像这样重写我的示例:上面还引用每个文件名来处理其中包含空格的名称,并修剪第一次循环迭代留下的字符串前面的额外空格
%LIST:~1%
。Near the end of the output from
SET /?
is this gem about delayed environment variable extraction. It shows how to use a relatively new notation (since NT 3.1? it works in XP and Win 7) for delayed expansion of an environment variable to build a list of file names matching a wild card in a single variable.Note that there is an issue here with quoting of names containing spaces or other "interesting" characters that I've left as an exercise for the student. Getting quoting right in CMD.EXE is even harder than getting it right in any Unix shell.
Naturally, replace the
echo
command with your command line.Edit: It has been observed that this doesn't seem to work as well in a batch file, and it depends on the specific feature of delayed expansion being enabled.
The delayed expansion feature is enabled with the
/V:ON
switch to CMD.EXE, or globally for all invocations of CMD by an registry key. The details are documented in the output ofCMD /?
.Moving to a batch file, you have a couple of issues, and an easy fix for enabling the feature. The key is that the
SETLOCAL
command has an option to turn the delay feature on and off at will. FromCMD /?
:Also, there is the cryptic need to double the percent signs in some contexts, such as
FOR
commands. All together, I'd rewrite my example like this:The above also quotes each file name to deal with names that have spaces in them, and trims the extra space off the front of the string that was left there by the first loop iteration with
%LIST:~1%
.