0

I want to concat each a.txt in C:, D:, and E:.

@echo off
setlocal EnableDelayedExpansion

set list=C:
set list=%list%;D:
set list=%list%;E:

set "file_list="

for %%f in (%list%) do (
    set "file_list=%file_list% %%f\a.txt"
)

echo "file_list=%file_list%"

I expect the output is file_list=C:\a.txt D:\a.txt E:\a.txt,

but the actual output is file_list= E:\a.txt

The topic Variables are not behaving as expected doesn't answner my question!

user1633272
  • 1,797
  • 5
  • 21
  • 42
  • Use delayed expansion of variable **file_list**. Change substring **%file_list%** to **!file_list!** in cycle. Topic **Variables are not behaving as expected** is yours – Daemon-5 Apr 12 '22 at 15:31
  • 2
    The linked duplicate perfectly answers your question: Don't just *enable* delayed expansion, also actually *use* it: `set "file_list=!file_list! %%f\a.txt"`. (if you don't want the leading space: `echo file-list=%file_list:~1%`) – Stephan Apr 12 '22 at 15:40
  • @Stephan it works now. I understand what do `delayed` and `parse time` mean now. Thanks. – user1633272 Apr 13 '22 at 03:10

0 Answers0