For loop not setting enviornment variable

October 8, 2010 at 09:03:35
Specs: Windows XP
Hey I have a batch script which is supposed to parse the second element of a line and set a variable to it.
FOR /F "usebackq TOKENS=2 DELIMS= " %%A IN ("C:\Temp\Log1.txt") DO SET VAR=%%A
ECHO.VAR is %VAR%
However, when I go to print it in the second statement, it doesnt print anything - meaning that the variable isnt set at all.

BUT if I replace the DO SET VAR=%%A with DO ECHO.A is %%A then it prints and shows the right value.

Why isnt the variable being set properly? Thankful for any help.


See More: For loop not setting enviornment variable

Report •

#1
October 8, 2010 at 10:08:56
I pasted in your FOR and it works.

Beats me.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
October 8, 2010 at 12:56:49
Question:

Are you using:

FOR /F "usebackq TOKENS=2 DELIMS= " %%A IN ("C:\Temp\Log1.txt") DO SET VAR=%%A
ECHO.VAR is %VAR%

Or are you using:
FOR /F "usebackq TOKENS=2 DELIMS= " %%A IN ("C:\Temp\Log1.txt") DO (
SET VAR=%%A
ECHO.VAR is %VAR%
)

How To Ask Questions The Smart Way


Report •

#3
October 8, 2010 at 12:59:18
The first one....but should it make a difference?

Report •

Related Solutions

#4
October 8, 2010 at 13:16:00
Short version: Yes.
Longer version: It would, because variable expansion happens before looping/executing grouped statements.
Longest version: SET /?

I suggest you leave ECHO ON, to see specifically what's happening.

How To Ask Questions The Smart Way


Report •

#5
October 8, 2010 at 14:26:38
Btw, I do happen to have this for loop within a nested for loop. I took out the inside for loop and it executes fine....but I want nested for loops, not serial for loops.

Are batch scripts not OK with nested for loops?


Report •

#6
October 8, 2010 at 14:31:18
If you want help with nested for loops it might help to post them.


=====================================
Helping others achieve escape felicity

M2


Report •

Ask Question