How to concatenate strings in a windows batch file?

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

I know the following command command: echos all files:

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

But now I want to put them all in a variable, add ; between them and I will repeat them all at once.
How can i do this?

 set myvar="the list: " for /r %%i In (*.doc) DO <what?> echo %myvar% 
+67
windows string-concatenation batch-file
Jan 08 '10 at 11:00
source share
3 answers

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 
+58
Jan 08
source share

Based on the Rubens solution, you need to enable Delayed Expansion of env variables (type "help setlocal" or "help cmd") so that var is correctly evaluated in the loop:

 @echo off setlocal enabledelayedexpansion set myvar=the list: for /r %%i In (*.sql) DO set myvar=!myvar! %%i, echo %myvar% 

Also consider the following restriction ( MSDN ):

The maximum individual environment variable size is 8192 bytes.

+43
Jan 08 '10 at 11:25
source share

Note that the @fname or @ext can simply be concatenated. It:

 forfiles /S /M *.pdf /C "CMD /C REN @path @fname_old.@ext" 

renames all PDF files to filename_old.pdf

0
Nov 12 '15 at 16:08
source share



All Articles