5

I'm trying to create a file list to pass to a program as an argument but i'm having problems with it.

My desired output should be: file1.jpg, file2.bmp, file3.docx

The bat i'm trying to use is this:

SETLOCAL EnableDelayedExpansion
cd /D %~dp0
set _filelist=
for /f "delims=|" %%f in ('dir /b %CD%') do (
  set "_filelist=%_filelist%,%%f"
)
echo %_filelist%
pause

But it only output the last file in the list: ,lastfile.xyz

What i'm doing wrong?

1
  • Sorry for the late reply. check the comment on your answer! – Vico Oct 23 '17 at 18:21
5

It seems like you simply forgot to use the exclamation mark ("!") with the variable you set within the loop, and to resolve to simply use !_filelist! rather than %_filelist%.

The below script will set the initial set _filelist=, variable with a single comma value so the first iteration will be ,, and then the first iterated value rather than the null plus one comma you get two back to back commas giving a unique pattern to work with for parsing.

You can use Variable Replace functionality to parse out the double commas (,,), and replace those with a blank value. The final iterated and set variable value will then be parsed from outside the loop with set _filelist=%_filelist:,,=% removing the double comma prefixed value.

Script

SETLOCAL EnableDelayedExpansion
cd /D %~dp0
set _filelist=,
for /f "delims=|" %%f in ('dir /b %CD%') do (
  set "_filelist=!_filelist!,%%f"
)
set _filelist=%_filelist:,,=%
echo %_filelist%
pause

Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command. [1]


When delayed expansion is in effect, variables can be immediately read using !variable_name! you can also still read and use %variable_name% that will show the initial value (expanded at the beginning of the line). [1]


Further Resources

5
  • Sorry for the late response, i was in a hurry here. Well, the script works like a charm, but before the file list i always get an extra , at the beginning of the line, like: ,file1.xxx,file2,xxx. Is possible to eliminate this very first comma? – Vico Oct 23 '17 at 18:20
  • Give me a few and I'll see what I can come up with. Have a few meetings so it may be a few hours. – Pillsbury IT Doughboy Oct 23 '17 at 18:21
  • Using this line only the last filename is returned. – Vico Oct 23 '17 at 18:28
  • The question is worse... In the first iteration obviously the filelist variable still havent any value, anyway the comma is added plus the very first filename in the list. So I need to figure out how to "filter" out the comma in that very first iteration. – Vico Oct 23 '17 at 18:30
  • @Vico See the amended Bonus Example section at the bottom explaining what I used, etc. but from what I tested, it works as expected. – Pillsbury IT Doughboy Oct 23 '17 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.