Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does "setlocal enabledelayedexpansion" not work ?

888 views
Skip to first unread message

John Kennicks

unread,
Dec 6, 2007, 6:30:31 AM12/6/07
to
In a batch file (under WinXP) I coded:

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

foxidrive

unread,
Dec 6, 2007, 6:31:59 AM12/6/07
to

use !odd! instead of %odd%

Ted Davis

unread,
Dec 6, 2007, 8:42:48 AM12/6/07
to

RTFM - SET /?

--
T.E.D. (tda...@umr.edu)

billious

unread,
Dec 6, 2007, 8:21:27 AM12/6/07
to

"John Kennicks" <jke...@hotmail.com> wrote in message
news:4757dd57$0$16667$9b4e...@newsspool3.arcor-online.net...

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.)


Tom Lavedas

unread,
Dec 6, 2007, 2:57:13 PM12/6/07
to

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/

Todd Vargo

unread,
Dec 6, 2007, 11:45:59 PM12/6/07
to
John Kennicks wrote

Because you are not using delayed expansion. Change %odd% to !odd!.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Message has been deleted
Message has been deleted

Todd Vargo

unread,
Dec 6, 2007, 11:57:22 PM12/6/07
to

Because you are not using delayed expansion. Change %odd% to !odd!.

0 new messages