Tutorial by Examples

call :FunctionX rem More code... :FunctionX rem Some code here. goto :eof This is a very simple function. Functions are in-program commands that do multiple commands at a time. Functions are made by creating a label and putting code in it, and once it is done, you add a goto :eof or exit ...
call :tohex 14 result rem More code... :tohex <innum> <outvar> set dec=%1 set outvar=%~2 rem %n and %~n are functionally identical, but %~n is slightly safer. goto :eof This takes the additional parameters from the call as if the function was a separate Batch file. Note: the ...
set var1=123456789 set var2=abcdef call :specialvars echo %var1%, %var2% rem More code... :specialvars setlocal set var1=987654321 set var2=fedcba endlocal goto :eof When inside the section setlocal , endlocal section, variables are seperate from the caller's variables, hence why %var...
set importantvar=importantstuff call :stuff 123 var1 rem More code... :stuff <arg1> <arg2> setlocal set importantvar=%~1 echo Writing some stuff into %~2! endlocal set %~2=some stuff setlocal set importantvar=junk endlocal goto :eof This utilizes the basic function, setl...
Anonymous functions technique uses the fact that CALL command uses internally GOTO when subroutine is called and abusing help message printing with variable double expansion: @echo off setlocal set "anonymous=/?" call :%%anonymous%% a b c 3>&1 >nul if "%0" == ...
Lets have the following file called library.cmd : @echo off echo -/-/- Batch Functions Library -/-/- :function1 echo argument1 - %1 goto :eof To execute only the :function1 without the code of the rest of the file you should put a label :function1 in the caller bat and use it lik...

Page 1 of 1