How to concatenate strings in a Windows batch file?

Fortega picture Fortega · Jan 8, 2010 · Viewed 203.5k times · Source

I have a directory for which I want to list all the .doc files with a ;.

I know the following batch command echos all the files:

for /r %%i In (*.doc) DO echo %%i

But now I want to put them all in a variable, add a ; in between and echo them all at once.
How can I do that?

set myvar="the list: "
for /r %%i In (*.doc) DO <what?>
echo %myvar%

Answer

Rubens Farias picture Rubens Farias · Jan 8, 2010

What about:

@echo off
set myvar="the list: "
for /r %%i in (*.doc) DO call :concat %%i
echo %myvar%
goto :eof

:concat
set myvar=%myvar% %1;
goto :eof