Kannan
Kannan

Reputation: 551

Concatenating a list in command prompt

I have a windows console application that accepts a list of files as parameter each separated by a space, like MyApp.exe 1.txt 2.txt 3.txt. My folder has other file formats in addition to txt, but I am interested only in txt files. I would like to achieve something like this MyApp.exe for %%i in (.\*.txt). Thanks in advance.

Upvotes: 3

Views: 87

Answers (1)

zb226
zb226

Reputation: 10500

The only peculiarity is that you need to enable delayed expansion - do it like this:

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

This will call:

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

If you want to strip any path info, use %%~nxI instead of %%I:

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

This will result in:

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

Be aware that there is 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.

Upvotes: 3

Related Questions