FOR issues

Jun 13, 2008
12
0
#1
  1. FOR can't be used in @EXEC. Example:
    Code:
    echo %@exec[@for ^%A in (123) echo %A] > nul
    returns "ECHO IS OFF".
  2. SWITCH works incorrectly from inside FOR loop: executing
    Code:
    for %A in (123) (
     switch a
     case a
      echos aaa
     case f
      echo fff
     endswitch
    )
    returns "aaafff".
 
Jun 13, 2008
12
0
#2
3. It seems that RETURN is not allowed in nested FOR loops: executing
Code:
gosub aaa
echo %bbb
quit
:aaa
for %A in (123) (
 for %B in (345) (
  return
 )
 set bbb=ccc
)
displays "ccc".
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,725
47
Albuquerque, NM
prospero.unm.edu
#3
FOR can't be used in @EXEC. Example:

Code:
echo %@exec[@for ^%A in (123) echo %A] > nul
returns "ECHO IS OFF".
I believe you need to double your percent signs here:

Code:
echo. %@exec[@for %%A in ( 123 ) echo %%A]
(The period after ECHO is a rather nasty way of preventing the "ECHO is OFF" message.)

SWITCH works incorrectly from inside FOR loop: executing
Code:
for %A in (123) (
 switch a
 case a
  echos aaa
 case f
  echo fff
 endswitch
)
returns "aaafff".
SWITCH is inherently a multiline construct, and FOR is inherently single-line. Use DO instead.
 
#4
On Fri, 13 Jun 2008 16:29:59 -0500, you wrote:


>echo %@exec[@for ^%A in (123) echo %A] > nul
>---------
>returns "ECHO IS OFF".
v:\> echo %@exec[for %%A in (123) echo %%A]
123
0

v:\> echo %@exec[@for %%A in (123) echo %%A]
123
ECHO is OFF

as expected.
 
Jun 13, 2008
12
0
#5
Charles Dye
I believe you need to double your percent signs here
Yeah, I'm wondering why I haven't tried this myself :)
SWITCH is inherently a multiline construct, and FOR is inherently single-line. Use DO instead.
Well IFF is another multiline construct, but it works inside FOR loop.

Anyway, I believe such things should be illustrated in Help.
 
#6
Raistlin wrote:
| *Charles Dye*
| ---Quote---
| I believe you need to double your percent signs here
| ---End Quote---
| Yeah, I'm wondering why I haven't tried this myself :)
|
| ---Quote---
| SWITCH is inherently a multiline construct, and FOR is inherently
| single-line. Use DO instead. ---End Quote---
| Well IFF is another multiline construct, but it works inside FOR loop.

Technically, DO and SWITCH are multiline commands; IFF is a multiple
command.

|
| Anyway, I believe such things should be illustrated in Help.

Agreed. Some elements are. Impossible to illustrate all possible variations.
SWITCH is documented to perform its actions "in a batch file". The existence
of limitations in using a command group in the DO clause of FOR is also
documented.
--
Steve