How to iterate through directory and concatenate names of folders into string

Similar to this question, but with folder names instead of with file names. The current code I have for concatenating files is

setlocal enabledelayedexpansion
set colorFileNames=
for %%f in (%CD%\automation\*) do (
  set _file=%%~nf
  if [!colorFileNames!]==[] (
    set colorFileNames=!_file!
    ) else (
    set colorFileNames=!colorFileNames!, !_file!
    )
)

which outputs something like fileName1, fileName2, ..., fileNameN but it obviously only works with files. How would I modify it to use folder names instead of file names?


Solution 1:

I changed a few things to make it work:

setlocal enabledelayedexpansion
set graphicsSets=
for /D %%f in (%CD%\automation\*) do (
  set _folder=%%~nf
  if [!graphicsSets!]==[] (
    set graphicsSets=!_folder!
    ) else (
    set graphicsSets=!graphicsSets!, !_folder!
    )
)

The /D tells the loop to go over directories instead of files, and I replaced what was in the () with just the path to the folder I was looping in. Everything else is the same, so not too difficult of a modification.