1

I have the following problem in batch scripting.

I have this code lines

for /f "tokens=*" %%d in ('dir /b /s a2lfiles_merger*.txt') do set merger_list=%%d
echo %merger_list%

and the result is Echo is OFF

Then i tried this code

for /f "tokens=*" %%d in ('dir /b /s a2lfiles_merger*.txt') do echo %%d

The result is C:\Users\user\Desktop\Build\Input\JOB_1\a2lfiles_merger.txt

So, the question is why i cannot set a variable with the path? I need to use it in the next steps and now i am stuck in this situation. Can anyone find a solution ? Thank you all in advance!

pop rock
  • 517
  • 1
  • 6
  • 14
  • 2
    Your first sample is in a code block / parenthesis. The `set` works but the percent expansion in the echo line fails. In this case you need to use delayed expansion – jeb Jul 30 '15 at 11:01
  • @jeb, the first line is _not_ a block as there are no parentheses; if no file `a2lfiles_merger*.txt` can be found, `echo` results in `Echo is off.`; if at least one file is found, the last one is displayed; so I guess it is a path issue (where is the code called from?)... – aschipfl Jul 30 '15 at 17:41
  • @jeb Thank you, it's working. Could you explain the diffirent between `! & %` – pop rock Jul 31 '15 at 06:40

1 Answers1

1

revised code:

    setlocal enabledelayedexpansion
    for /f "tokens=*" %%d in ('dir /b /s a2lfiles_merger*.txt') do (
set merger_list=%%d
    echo !merger_list!
)

note the setlocal command and the parenthesis enclosing the commands to be executed in the for-loop. that's how for loops work.

hope that helped!

Jahwi
  • 426
  • 3
  • 14