setlocal enabledelayedexpansion
set odd=y
for %%a in (D:\test\*.log) do (
if %odd%==y (
echo file is odd
set odd=n
) else (
echo file is even
set odd=y) )
However the variable odd remain always "y"
Why does it never switch to n ?
John
use !odd! instead of %odd%
Best to use alt.msdos.batch.nt for the NT-series as NT concepts differ from
DOS/9x. - Oh, Isee - naughty crossposting.....
This is a FAQ in that group (in various guises.) DOS parses the entire
LOGICAL command-line (from the FOR to the closing parenthesis) and
immediately subsitutes the CURRENT (ie. PARSE-TIME) value of
%anyvariablename% THEN the resultant command is executed, consequently your
command would be executed as
for %%a in (D:\test\*.log) do (
if y==y (
echo file is odd
set odd=n
) else (
echo file is even
set odd=y) )
so, had you added the line
ECHO the final value of ODD is %odd% - isn't that odd?
you'd find that the value of ODD has indeed changed.
SETLOCAL enabledelayedexpansion
allows you to use !varname! in place of %varname% to obtain the dynamic
value of the variable, so replacing %odd% by !odd! in 'if %odd%==y (' should
give the behaviour you expect.
BUT - there's a trap.
SETLOCAL forces all variables to become 'local' and consequently if this is
a CALLED batch, the changes will NOT affect the callING environment.
To force the changes to stick, you need to use the matching ENDLOCAL with
SET(s) in the same logical line, and exploit the same characteristic
set odd=oldvalue
SETLOCAL enabledelayedexpansion
set odd=newvalue
ENDLOCAL&set odd=%odd%
echo value of odd is %odd%
If you omit the "&set..." then you'll find that ODD will have value
oldvalue.
If you omit the entire ENDLOCAL line, ODD will be reported as newvalue by
the ECHO, but will be shown as oldvalue once the routine has terminated (eg
ig you use a
SET odd
command, or you CALL this routine from another batch.)
Replace the % with the delayed expansion marker, ! ...
setlocal enabledelayedexpansion
set odd=y
for %%a in (D:\test\*.log) do (
if !odd!==y (
echo file is odd
set odd=n
) else (
echo file is even
set odd=y) )
Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
Because you are not using delayed expansion. Change %odd% to !odd!.
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
Because you are not using delayed expansion. Change %odd% to !odd!.