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

Argument Oddity: shift within IF block does not take effect within IF block

25 views
Skip to first unread message

rpre...@gmail.com

unread,
Apr 12, 2012, 4:12:47 PM4/12/12
to
Discovered on Windows 7 Ultimate
Verified same on Windows 2008 Server and Windows 2003 Server

Consider this batch file:

@ECHO OFF
Rem Beginning of test.cmd ==========
:LOOP
IF .Ross==.Ross (
shift
echo .%1
)
echo .%1
shift
echo .%1
REM EOF ==========

One would expect the output of
test.cmd first second third
to be
.second
.second
.third

Instead it is
.first
.second
.third

The shift within the IF block is noticed, but it does not affect the value of %1 until the IF block is over.

If the lines "If .Ross==.Ross" and ")" are rem'd out, then the expected output is obtained.

Is this documented anywhere?


billious

unread,
Apr 12, 2012, 4:57:25 PM4/12/12
to

<rpre...@gmail.com> wrote in message
news:19381814.258.1334261567765.JavaMail.geo-discussion-forums@vbyz30...
Yes. Repeatedly in this group.

When the IF statement is parsed - and that's the entire logical statement,
not the physical line alone - the then-current value of any %-variable other
than a metavariable (ie loop-control variable) is substituted for the
variable. THEN the resultant statement is executed.

hence, the IF statement is executed as

IF .Ross==.Ross (
shift
echo .first
)

by substituting the value of %1 into the statement at PARSE time, not
execution time.

Similarly,

IF .Ross==.Ross (
shift
echo .%1
shift
echo .%1
)

woud show ".first" twice - same reason.


rpre...@gmail.com

unread,
Apr 13, 2012, 10:55:36 PM4/13/12
to
On Thursday, April 12, 2012 4:57:25 PM UTC-4, billious wrote:

> Yes. Repeatedly in this group.

Thanks. I guess I just didn't have enough google-fu to find it
in this group. I wish it were explicitly mentioned somewhere
on MSDN.
0 new messages