@echo off
cls
echo Subroutines in DOS batch files proof-of-concept 0.02
echo by Daren Freckleton August 25, 2000
echo.
echo tweaked by tehdr33mah, who just _loves_ that GOTO ENVAR
echo.
rem -----------------------------------------------------------------
rem This algorithm can repeatedly call a generic subroutine embedded
rem within the same DOS batch file. This could be useful if, for
rem example, you have a list of items for processing by the same set
rem of DOS batch commands.
rem It works by setting a control variable, in addition to any
rem variables required by the subroutine. This variable is a pointer
rem to the routine that called the subroutine - in fact it is the
rem name of a ':label' embedded in the calling subroutine. The
rem calling routine, which has the embedded label, sets the control
rem variable RETURN to its label, sets any variables for the subroutine
rem to process, and then calls the subroutine. The subroutine executes
rem as usual, but on ending executes a GOTO %RETURN%. This causes
rem execution to jump to the label embedded in the calling routine.
rem Execution can then continue from where the call was made.
rem The algorithm thus consists of two parts, a section of the calling
rem routine, and the final GOTO in the subroutine.
rem ------------------------------------------------------------------
set RETURN=item-a
set IPADDRESS=192.168.0.1
goto subroutine
:item-a
set RETURN=item-b
set IPADDRESS=192.168.0.2
goto subroutine
:item-b
set RETURN=item-c
set IPADDRESS=192.168.0.3
goto subroutine
:item-c
goto end
:subroutine
echo pinging %IPADDRESS% ...
ping -n 1 %IPADDRESS% | find /i "packets:"
goto %RETURN%
:end
echo.