0

I have created a code that takes the following variables:

SET sdir=T:\path\to\in\
SET tempdir=T:\path\to\tempBatch\
SET list=DE NL

Then, I try to loop through the list items and copy all TXT files in them to the tempBatch folder.

(for %%l in (%list%) do (
    set tempINdir=%sdir%%%l
    echo %%l
    echo %tempINdir%
))

The output I get is:

DE
T:\path\to\in\NL
NL
T:\path\to\in\NL

Of course, I want to have the %%l variable concatenate with the %sdir% path:

The output I get is:

DE
T:\path\to\in\DE
NL
T:\path\to\in\NL

Why does it only take the last item in the list when creating tempINdir? I have tried to use setlocal EnableDelayedExpansion from this answer, but this doesn't do anything.

Community
  • 1
  • 1
Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29
  • 4
    *enabling* [delayed expanison](http://stackoverflow.com/a/30284028/2152082) is not enough. You also have to *use* it: `echo !tempINdir!` – Stephan Sep 05 '16 at 09:47
  • Possible duplicate of [Batch script for loop won't set variable](http://stackoverflow.com/questions/12518242/batch-script-for-loop-wont-set-variable) – aschipfl Sep 05 '16 at 10:21
  • @aschipfl, as said in the question: that answer did not help me. – Nander Speerstra Sep 05 '16 at 10:51
  • @Stephan, thanks, that works! – Nander Speerstra Sep 05 '16 at 10:51
  • The linked answer perfectly demonstrates both to enable and to use delayed expansion in order to resolve exactly the same problem... – aschipfl Sep 05 '16 at 10:53
  • No it _shows_ it and it doesn't explain it. It is my non-understanding of the batch syntax that I did not see the different reference characters. Which is why I asked it here. Here it is explained. – Nander Speerstra Sep 05 '16 at 10:56

1 Answers1

0

As explained in the comments by Stephan and in this answer:

Delayed variables are referenced with !var! instead of %var%

So, in the for loop you need to use !var! instead of %var%.

Community
  • 1
  • 1
Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29