在命令提示符中连接列表

Concatenating a list in command prompt

我有一个 windows 控制台应用程序,它接受文件列表作为参数,每个文件由 space 分隔,例如 MyApp.exe 1.txt 2.txt 3.txt。我的文件夹除了txt还有其他文件格式,但我只对txt文件感兴趣。我想实现这样的目标 MyApp.exe for %%i in (.\*.txt)。提前致谢。

唯一的特点是您需要启用 delayed expansion - 这样做:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR %%I IN (.\*.txt) DO SET files=!files! %%I
MyApp.exe !files!

这将调用:

MyApp.exe ..txt ..txt ..txt

如果要删除任何路径信息,请使用 %%~nxI 而不是 %%I:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR %%I IN (.\*.txt) DO SET files=!files! %%~nxI
MyApp.exe !files!

这将导致:

MyApp.exe 1.txt 2.txt 3.txt

注意有a limit on the length of the command:

On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters.