For loop not processing as expected

S

stanps

Can someone PLEASE tell me why the following 'for loop' only processes
the last character of the group?

for %%a in (j r) do (
echo %%a
if /i "%%a"=="r" (set thisdir=c:\Windows)
if /i "%%a"=="j" (set thisdir=c:\temp)
)

This will always return with %thisdir% = c:\Windows

If I make 'j' the last character in the group, it will always return
%thisdir% = c:\temp.

Please note: I can see it step through the loop using echo commands,
however the thisdir variable is only set at the end. Just cut and
paste it and run it, and you'll see what I'm talking about.

Thank you VERY much, in advance!
Stan :)
 
M

Matthias Tacke

stanps said:
Can someone PLEASE tell me why the following 'for loop' only processes
the last character of the group?

for %%a in (j r) do (
echo %%a
if /i "%%a"=="r" (set thisdir=c:\Windows)
if /i "%%a"=="j" (set thisdir=c:\temp)
)

This will always return with %thisdir% = c:\Windows

If I make 'j' the last character in the group, it will always return
%thisdir% = c:\temp.

Please note: I can see it step through the loop using echo commands,
however the thisdir variable is only set at the end. Just cut and
paste it and run it, and you'll see what I'm talking about.
Hi Stan,
you explain the expected behavior yourself - so what is your problem?
After the for is executed the variable thisdir can hold only one value, the
most recent one.

And if you insert an echo %thisdir% inside the parentheses you have a
problem with delayed expansion - see "setlocal /?"
 
B

billious

stanps said:
Can someone PLEASE tell me why the following 'for loop' only processes
the last character of the group?

for %%a in (j r) do (
echo %%a
if /i "%%a"=="r" (set thisdir=c:\Windows)
if /i "%%a"=="j" (set thisdir=c:\temp)
)

This will always return with %thisdir% = c:\Windows

If I make 'j' the last character in the group, it will always return
%thisdir% = c:\temp.

Please note: I can see it step through the loop using echo commands,
however the thisdir variable is only set at the end. Just cut and
paste it and run it, and you'll see what I'm talking about.

Thank you VERY much, in advance!
Stan :)

I suspect this is part of an unstated larger problem.

Since thisdir can only contain one value, what did you expect it to contain?
If it always contained the FIRST value of the group, no doubt you'd complain
about that!

I suspect you also tried

for %%a in (j r) do (
echo %%a
if /i "%%a"=="r" (set thisdir=c:\Windows)
if /i "%%a"=="j" (set thisdir=c:\temp)
ECHO thisdir=%thisdir%
)
ECHO thisdir AFTER =%thisdir%

and were surprised to find the output lines

thisdir=
thisdir=
thisdir AFTER =c:\Windows

and thus see no way of finding the dynamic value of thisdir.

try:
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (j r) do (
echo %%a
if /i "%%a"=="r" (set thisdir=c:\Windows)
if /i "%%a"=="j" (set thisdir=c:\temp)
ECHO thisdir=!thisdir!
)
ECHO thisdir AFTER =%thisdir%

noting the SETLOCAL command (and its argument) and reading the (obscure)
documentation about SETLOCAL which can be found by executing the command

SETLOCAL /?

from the prompt.

Note also the use of !thisdir! rather than %thisdir% within the loop. This
shows the dynamic or instantaneous value of the variable (!varname!) rather
than the value when the LOGICAL command-line was PARSED (%varname%) but ONLY
if ENABLEDELAYEDEXPANSION is in effect, and THAT requires SETLOCAL to be in
effect also.

It might be an idea to look into the newsgroup alt.msdos.batch.nt for many,
many, more examples and quirks.
 
S

stanps

Hi Stan,
you explain the expected behavior yourself - so what is your problem?
After the for is executed the variable thisdir can hold only one value, the
most recent one.

And if you insert an echo %thisdir% inside the parentheses you have a
problem with delayed expansion - see "setlocal /?"

--
Greetings
Matthias- Hide quoted text -

- Show quoted text -

Excellent! Thank you...I'll check it out.
S :)
 
S

stanps

I suspect this is part of an unstated larger problem.

Since thisdir can only contain one value, what did you expect it to contain?
If it always contained the FIRST value of the group, no doubt you'd complain
about that!

I suspect you also tried

for %%a in (j r) do (
echo %%a
if /i "%%a"=="r" (set thisdir=c:\Windows)
if /i "%%a"=="j" (set thisdir=c:\temp)
ECHO thisdir=%thisdir%
)
ECHO thisdir AFTER =%thisdir%

and were surprised to find the output lines

thisdir=
thisdir=
thisdir AFTER =c:\Windows

and thus see no way of finding the dynamic value of thisdir.

try:
SETLOCAL ENABLEDELAYEDEXPANSION
for %%a in (j r) do (
echo %%a
if /i "%%a"=="r" (set thisdir=c:\Windows)
if /i "%%a"=="j" (set thisdir=c:\temp)
ECHO thisdir=!thisdir!
)
ECHO thisdir AFTER =%thisdir%

noting the SETLOCAL command (and its argument) and reading the (obscure)
documentation about SETLOCAL which can be found by executing the command

SETLOCAL /?

from the prompt.

Note also the use of !thisdir! rather than %thisdir% within the loop. This
shows the dynamic or instantaneous value of the variable (!varname!) rather
than the value when the LOGICAL command-line was PARSED (%varname%) but ONLY
if ENABLEDELAYEDEXPANSION is in effect, and THAT requires SETLOCAL to be in
effect also.

It might be an idea to look into the newsgroup alt.msdos.batch.nt for many,
many, more examples and quirks.- Hide quoted text -

- Show quoted text -

You are correct! I'm wanting to create a loop which will copy
different types of files to different destination directories. This
is only the first part...without this working, there's no point in
continuing. :)

Thanks again!
Stan :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top