Question about using for loop in DOS batch fi

December 15, 2010 at 08:11:38
Specs: Windows XP
Hi, I'm trying to write a DOS batch file with a FOR loop. Inside the FOR block, I need to do assignment on some variables but it always fails to work. I have wrote a simple test file like this:

@echo off

set str=A
for /f "tokens=* delims= " %%i in (test.txt) do (
echo OK
set str=B
echo %str%
)
I was expecting to see result like:
OK
B
OK
B
OK
B

However, the result looks like:
OK
A
OK
A
OK
A


From the result, it seems ignored the assignment. I really have no clue what happens here and how to fix it. Could you please help me on this?

Thank you very much.


Jason


See More: Question about using for loop in DOS batch fi

Report •


#1
December 15, 2010 at 09:53:11
Required reading:
SET /?

How To Ask Questions The Smart Way


Report •

#2
December 15, 2010 at 13:36:58
Jason - also read up on enabling delayed expansion.

@echo off
setlocal enabledelayedexpansion

set str=A
for /f "tokens=* delims= " %%i in (test.txt) do (
echo OK
set str=B
echo !str!
)

Good luck.

Did you Google for an answer before asking the question?


Report •

Related Solutions


Ask Question