\[Editor’s Note: Some or all of the following FAQ text was submitted by readers, Rob Warner and Ian Hamilton.\]

A. An easy solution is to have the batch file call itself recursively and pass itself a couple of parameters, as the following example illustrates.

@echo off<br>
if (%1)==(Recurse) goto Recurse<br>
goto Begin<br><br>
:Begin<br>
echo Batch file begins.<br>
call %0 Recurse test<br>
goto End<br><br>
:Recurse<br>
echo This is a recursive call.<br>
echo The parameters received were "%1" and "%2".<br>
goto Clean-End<br><br>
:End<br>
echo Finished.<br><br>
:Clean-End


Be careful using this method, because recursive batch files can be dangerous if your subroutine fires off another program. The following example illustrates an alternative method.

:Begin<br>
echo start subroutine<br>
call :Subroutine<br>
echo finished subroutine<br>
goto End<br>
:Subroutine<br>
echo In subroutine<br>
goto :EOF<br>
:End

The following example illustrates yet another method, which you can use on Windows NT .cmd files. Note the syntax for calling subroutines (with parameters) and the special construction for returning (i.e., goto :eof).

@echo off<br>
call :Begin<br>
echo Finished.<br>
goto :eof<br>
:Begin<br>
echo Batch file begins.<br>
call :recurse Recurse test<br>
goto :eof<br>
:Recurse<br>
echo This is a recursive call.<br>
echo The parameters received were "%1" and "%2".<br>
goto :eof