Batch file – Write list of files to variable

Pretty simple:

setlocal enabledelayedexpansion enableextensions
set LIST=
for %%x in (%baseDir%\*) do set LIST=!LIST! %%x
set LIST=%LIST:~1%

In fact, you find this very example also in the help for the set command, accessible via help set, complete with an explanation why the naïve approach won’t work.

To use a different set of files (rather than all), you can easily change the wildcard:

for %%x in (%baseDir%\*.c) do set LIST=!LIST! %%x

Leave a Comment