Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Consider this as the contents of a .cmd file:

@echo off
set test1=hello
echo 0 %test1%
for %%F in ( *.txt ) do (
  set test2=%%F
  echo 1 %test1%
  echo 2 %test2%
  echo 3 %%F
)
pause

When I run this script in Win 7 from a folder containing a.txt and b.txt as the only .txt files, I get this:

0 hello
1 hello
2
3 a.txt
1 hello
2
3 b.txt
Press any key to continue . . .

All the "successful" echo commands (starting with 0, 1 and 3) are proof that the echo and variable syntax are correct. The "unsuccessful" echo command (2) can only be the result of an unsuccessful SET command. How? Why? What SET command syntax do I need so that the test2 variable is assigned correctly, and the output lines starting with 2 are identical to those starting with 3?

share|improve this question
    
If you had done some research around, you'd see numerous related questions that are already answered. For example, this one. –  nerdwaller Jan 8 at 23:58
    
I've now spent 3 hours of working and reading on this. Believe me - it is not the slightest bit obvious unless you already know the answer. –  omatai Jan 9 at 0:01
    
So you're saying that you've tried the answers there and they did not work? –  nerdwaller Jan 9 at 0:06
    
No - what I was saying was that identifying what the problem is can be very hard. For example, none of the keywords I searched with showed up the answer you referred to, nor was it an answer suggested to me as I typed in the question. I found out that FOR loops had some issues around variable expansion (hence I put it in the title), but it was not clear to me if that was the issue, or the SET was the issue, or the use of the delimiters was the issue, or something else. It is very hard to search for delimiters!! –  omatai Jan 9 at 0:46
1  
The answer is also buried in the middle of the SET help: set /? or help set. But I imagine most people do not bother reading it, and even if they do, it is easy to miss unless you know what you are looking for. –  dbenham Jan 9 at 3:57

1 Answer 1

up vote 1 down vote accepted

The SET command is entirely fine. What is happening is that all of the DO block in the FOR command has its variables substituted when it is read the first time. At that point, the test2 variable is empty, so that is what is echoed in the output.

To fix this, you need to use a SETLOCAL EnableDelayedExpansion prior to the for loop, and change the echo %test2% to echo !test2! as follows:

@echo off
set test1=hello
echo 0 %test1%
setlocal enabledelayedexpansion
for %%F in ( *.txt ) do (
  set test2=%%F
  echo 1 %test1%
  echo 2 !test2!
  echo 3 %%F
)
endlocal
pause
share|improve this answer
    
you have your answer faster and better +1 –  STTR Jan 9 at 1:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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