Dos - Call (Subroutine, function)

About

The command “call” calls:

  • one batch script passing the (variable|execution) context (the environment variable)
  • a label (ie function)

The START command create a new cmd.exe process with then a new context (new environment variable) (with the /b option it will not open a new window)

Syntax

Call a file

CALL filePathName arguments

where:

Call a label

The CALL command now accepts labels as the target of the CALL.

CALL :label arguments

:label
REM exit the subroutine 
goto :EOF

where:

  • goto :eof exit the subroutine

Example

Label

@echo off

call :subroutine %*
exit /b %ERRORLEVEL%

:subroutine
echo Hello %*
goto :eof

:subroutine2
echo We will never see this

On the console:

echo.bat Nico
Hello Nico

Powered by ComboStrap