在 Windows 批处理文件中作为参数传递给 exe 时扩展 *.*

发布于 2024-10-10 02:39:09 字数 307 浏览 0 评论 0原文

我正在 Windows XP 中编写一个批处理文件。我已将一组 *.ts 文件复制到我的 exe 目录中。 TS 文件的数量和名称一样不固定。

现在我想运行我的 exe 之一,它将所有 TS 名称作为参数。

在Linux中我尝试过像 ; *.ts

这有效。但是当我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七婞 2024-10-17 02:39:09

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.

掩于岁月 2024-10-17 02:39:09

您可以使用 FOR 循环来枚举所有 *.ts 文件,例如

for %%f in (*.ts) do echo %%f

You could use a FOR-Loop to enumarate all *.ts files, like

for %%f in (*.ts) do echo %%f
季末如歌 2024-10-17 02:39:09

SET /? 输出的末尾附近是关于延迟环境变量提取的宝石。它展示了如何使用相对较新的符号(从 NT 3.1 开始?它适用于 XP 和 Win 7)来延迟扩展环境变量,以构建与单个变量中的通配符匹配的文件名列表。

延迟环境变量扩展允许您使用不同的
字符(感叹号)来扩展环境变量
执行时间。如果启用延迟变量扩展,则上述
示例可以编写如下以按预期工作:

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%

请注意,这里存在一个问题,即引用包含空格或其他“有趣”字符的名称,我将这些字符留给学生作为练习。在 CMD.EXE 中正确引用甚至比在任何 Unix shell 中正确引用还要困难。

当然,用您的命令行替换 echo 命令。

编辑:据观察,这在批处理文件中似乎效果不佳,并且它取决于启用的延迟扩展的特定功能。

延迟扩展功能通过 CMD.EXE 的 /V:ON 开关启用,或者通过注册表项全局调用 CMD。详细信息记录在 CMD /? 的输出中。

转移到批处理文件后,您会遇到一些问题,并且可以轻松修复以启用该功能。关键是SETLOCAL命令有一个可以随意打开和关闭延迟功能的选项。来自CMD /?

在批处理文件中,SETLOCAL ENABLEDELAYEDEXPANSIONDISABLEDELAYEDEXPANSION
参数优先于 /V:ON/V:OFF 开关。请参阅SETLOCAL /?
了解详情。

此外,在某些上下文中,例如 FOR 命令,还需要将百分号加倍。总之,我会像这样重写我的示例:

SETLOCAL ENABLEDELAYEDEXPANSION 
set LIST=
for %%f in (*.ts) do set LIST=!LIST! "%%f"
echo %LIST:~1%

上面还引用每个文件名来处理其中包含空格的名称,并修剪第一次循环迭代留下的字符串前面的额外空格%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.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%

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 of CMD /?.

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. From CMD /?:

In a batch file the SETLOCAL ENABLEDELAYEDEXPANSION or DISABLEDELAYEDEXPANSION
arguments takes precedence over the /V:ON or /V:OFF switch. See SETLOCAL /?
for details.

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:

SETLOCAL ENABLEDELAYEDEXPANSION 
set LIST=
for %%f in (*.ts) do set LIST=!LIST! "%%f"
echo %LIST:~1%

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文