Page 1 of 1

variable in cycle

Posted: 09 Jun 2010 03:07
by JustAlex
hello!
i have the next code:

Code: Select all

FOR /f "tokens=1 usebackq delims=," %%a in (`dsquery computer "OU=Users PC,OU=Computers,OU=Office,DC=gpi,DC=domain,DC=com"`) do (
rem set /a zzz=%zzz%+1
set zzz=%%a
   echo %zzz%
   echo %%a

)
pause

when it's running, i get the right value of %%a for all steps in cycle,
but %zzz% is always empty...

what's wrong?
have i to set zzz as global variable? how?
or the syntax for set zzz=%%a is incorrect?

Re: variable in cycle

Posted: 09 Jun 2010 08:43
by jeb
Hi,

zzz is not empty, but the way you try to print it fails.

The reason is the expansion phase of the %zzz%.
The %zzz% is only once expanded, at the beginning of the block ().
So you only see the value before you entered the block.

If you want to use the current value use the delayed expansion syntax.

Code: Select all

set EnableDelayedExpansion 
set zzz=Before
FOR /f "tokens=1 usebackq delims=," %%a in (`dsquery computer "OU=Users PC,OU=Computers,OU=Office,DC=gpi,DC=domain,DC=com"`) do (
rem set /a zzz=!zzz!+1
set zzz=%%a
   echo current=!zzz!, and %zzz%
   echo %%a
)
pause


jeb

Re: variable in cycle

Posted: 09 Jun 2010 10:22
by aGerman
@jeb
First line:
setlocal EnableDelayedExpansion
:wink:

Regards
aGerman

Re: variable in cycle

Posted: 10 Jun 2010 11:08
by jeb
You got me. :)

You can see, I haven't tested it.

jeb