views:

47

answers:

1

I'd like to get a list of all files (including their absolute path) into a variable, separated by spaces. My Google-fu seems to be weak in this regard, because I keep running into issues.

I have a base directory stored in %baseDir%, and would like to parse it for files (not recursing or including sub-directories). Like I mentioned, this needs to go into a list. I'd imagine there's a nice little shortcut I could use, but a for loop and concatenation would do the trick as well.

Ideally, I'd have something like this:

echo fileList

C:\file1.c C:\file2.c C:\file3.c
+1  A: 

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
Joey
simple question, simple answer...thanks!
espais
@espais: It's a pretty common one; I think I answered it roughly a dozen times by now ;-)
Joey
@Johannes...quick followup...is it easy to restrict this snippet to files of only *.c type?
espais
@espais: Of course: Just use `%baseDir%\*.c` as the wildcard.
Joey
thanks again!!!
espais
Ok, comment formatting stupidity strikes again; yay. I edited it into the answer, now without the backslash gone missing.
Joey