Code: Select all
@echo off
for /d %%a in (1 2 3 4 5) do (
if [%%a]==[0] (
call :INSIDE&& goto :eof
:INSIDE
echo.Inside the for loop
goto :eof
) else echo.%%a
)
Change 0 to 1-5 to see the logic working
Moderator: DosItHelp
Code: Select all
@echo off
for /d %%a in (1 2 3 4 5) do (
if [%%a]==[0] (
call :INSIDE&& goto :eof
:INSIDE
echo.Inside the for loop
goto :eof
) else echo.%%a
)
Code: Select all
@echo off
cls
call :whackyLoop "Whacky Loop"
echo.
echo.
echo.
call :coolLoop "Cool Loop"
exit /b
:whackyLoop
echo before loop (%~1)
for /d %%a in (1 2 3 4 5) do (
echo =============
echo %%a
if [%%a] geq [3] (
call :INSIDE %%a
echo before INSIDE label
:INSIDE
echo.Inside the loop %%a ^(%~1^)
)
)
echo -------------
echo after loop (%~1)
exit /b
:coolLoop
echo before loop (%~1)
for /d %%a in (1 2 3 4 5) do (
echo --------------
echo %%a
if [%%a] geq [3] call :INSIDE%%a %%a
if 1==0 (
:INSIDE3
echo Process %%a ^(%1^)
exit /b
:INSIDE4
echo Different Process %%a ^(%1^)
exit /b
:INSIDE5
echo Final Process %%a ^(%1^)
exit /b
)
)
echo ----------------
echo after loop (%~1)
exit /b
Code: Select all
before loop (Whacky Loop)
=============
1
=============
2
=============
3
Inside the loop %a (3)
-------------
after loop (3)
before INSIDE label
Inside the loop 3 (Whacky Loop)
=============
4
Inside the loop %a (4)
-------------
after loop (4)
before INSIDE label
Inside the loop 4 (Whacky Loop)
=============
5
Inside the loop %a (5)
-------------
after loop (5)
before INSIDE label
Inside the loop 5 (Whacky Loop)
-------------
after loop (Whacky Loop)
before loop (Cool Loop)
--------------
1
--------------
2
--------------
3
Process %a (3)
--------------
4
Different Process %a (4)
--------------
5
Final Process %a (5)
----------------
after loop (Cool Loop)
dbenham wrote:The Cool Loop is well behaved, but I can not see any benefit to the technique, given that the call loses the FOR loop variable context. And I am pretty sure that it is not calling the function from memory - I think it is still scanning the batch file for each call.
Code: Select all
@setlocal enableDelayedExpansion & echo off & goto :main
:err [3]
for /f %%n in ('set/a %~1 + 3') do exit /b %%n &rem 3rd choice
:err [4]
for /f %%n in ('set/a %~1 + 4') do exit /b %%n &rem 4th choice
:main
echo(
for %%n in (100 200) do (
if 1 equ 0 (
:err [5]
for /f %%n in ('set/a %~1 + 5') do exit /b %%n &rem 5th choice
)
call :err %%n
echo [%%n] !errorlevel!
call :err %%n
echo [%%n] !errorlevel!
if 1 equ 0 (
:err [6]
for /f %%n in ('set/a %~1 + 6') do exit /b %%n &rem 6th choice
)
)
set "@err=:err"
echo(
call; & call %@err%
echo call %%@err%% = call %@err% --^> errorlevel !errorlevel!
call; & call !@err!
echo call ^^!@err^^! = call !@err! --^> errorlevel !errorlevel!
%@err% &rem never call'd
!@err!
echo(
echo *** label injection failed ^^!? *** [%0]
endlocal & goto :eof
:err [1]
for /f %%n in ('set/a %~1 + 1') do exit /b %%n &rem 1st choice
:err [2]
for /f %%n in ('set/a %~1 + 2') do exit /b %%n &rem 2nd choice
Code: Select all
C:\tmp>in-call
[100] 101
[100] 101
[200] 201
[200] 201
call %@err% = call :err --> errorlevel 1
call !@err! = call :err --> errorlevel 1
*** label injection failed !? *** [in-call]
C:\tmp>
Aacini wrote:When a GOTO command is executed the target label is searched from the following line until the end of the file. If the label is not found, the search continue from the beginning of the file until the line of the original GOTO; at that point the "Label not found" error is issued. The first label found using this scheme is the target of the GOTO.
Code: Select all
@setlocal enableDelayedExpansion & echo off
(
if 1 equ 0 (
:err [2]
echo err [2] & goto :eof
)
goto :err
if 1 equ 0 (
:err [3]
echo err [3] & goto :eof
)
)
:err [1]
echo err [1] & goto :eof
endlocal & goto :eof
Liviu wrote:Aacini wrote:When a GOTO command is executed the target label is searched from the following line until the end of the file. If the label is not found, the search continue from the beginning of the file until the line of the original GOTO; at that point the "Label not found" error is issued. The first label found using this scheme is the target of the GOTO.
Right, with the observation that the search is in terms of parsed/logical lines, not physical lines - for example a parenthesized block counts as one logical line.
Liviu
Code: Select all
@echo off
set true=1 equ 1
if %true% (
goto dest
:dest
echo not here
if %true% (
:dest
echo not here
if %true% (
:dest
echo not here
if %true% (
:dest
echo not here
)
)
)
)
:dest
echo BUT HERE!
Code: Select all
@echo off
goto :block
:Label1
echo Label1 at Line4
exit /b
:block
(
call :label1
call :label2
exit /b
:label1
echo Label1 inside the block
exit /b
:label2
echo Label2 inside the block
exit /b
)
Output wrote:Label1 at Line4
Label2 inside the block
Code: Select all
@echo off
echo call the label
call :label
echo Returned from call
echo Jump into the function directly
:label This is a multiline ^
echo This will be displayed only when the label is called
echo This line will be displayed always
echo(
exit /b