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.

6
  • 4
    enabling delayed expanison is not enough. You also have to use it: echo !tempINdir! Commented Sep 5, 2016 at 9:47
  • Possible duplicate of Batch script for loop won't set variable Commented Sep 5, 2016 at 10:21
  • @aschipfl, as said in the question: that answer did not help me. Commented Sep 5, 2016 at 10:51
  • @Stephan, thanks, that works! Commented Sep 5, 2016 at 10:51
  • The linked answer perfectly demonstrates both to enable and to use delayed expansion in order to resolve exactly the same problem... Commented Sep 5, 2016 at 10:53

1 Answer 1

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%.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.