batch-file Functions Anonymous functions in batch files

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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" == ":%anonymous%" (
  echo(
  echo Anonymous call:
  echo %%1=%1 %%2=%2 %%3=%3
  exit /b 0
)>&3

You can call an anonymous function only if it is defined after the CALL (or after finishing brackets context if the CALL is executed within brackets). It cannot be called from an outside script ,but is a slower than normal function call.



Got any batch-file Question?