Batch file macros
A macro allows you to embed blocks of code in a variable.
Like calling a subroutine or function this allows reusing the same
block of code multiple times, the difference is that by placing the code in a
variable the performance will be much faster.This is an advanced technique but can be very useful in scripts that include large
blocks of code or looping commands where the performance of other methods is too slow.Passing arguments to a macro is not particularly easy, the best method (discovered by Jeb)
is to place a For /L command within the macro, set to run 2 steps each time the macro is run:
- In the first step the arguments are stored in a variable (argv)
- In the second step the main body of the macro runs and can (optionally) read the variable
Taked from ss64.com/nt/syntax-macro
In Seta Engines, macro usage is extended, boosting call to functions.
For more easy handle with macros, there is a :AutoMacro function that reads and compile
macros for you.
File self-explaining
@Echo Off
:::::::::::: You SHOULD include license here :::::::::::::::
Setlocal EnableExtensions DisableDelayedExpansion
:::: { Extension wrapping, eg: Seta:Dsp } ::::
:::::: { Flush unused variables, initializing } ::::::::
Call :Flush
Cd "%~dp0"
Set "Game=%~0"
Set LF=^
:: Above two lines are critical, do not remove!
:::::::::::::: { Macro Wrapping } ::::::::::::
:: Macro name, sensitive case
Set "Macros=MyMacro;MyOtherMacro"
::: Macro name cannot include spaces, just letters and numbers
For %%# in (%Macros%) Do (
Call :AutoMacro %%#
)
Setlocal EnabledelayedExpansion
For %%# in (%Macros%) Do (
Set ^"%%#=!%%#:[LF]=^%LF%%LF%!"
)
Set "Macros="
::::::::::::{ Ready for use macros, put code below }:::::::::
%MyMacro%
Echo;Hello World
%MyOtherMacro% First Second Third 4th 5th 6th 7th 8th 9th
Pause
Exit
::::::::::::::::::::::{ Macro Area }:::::::::::::::::::::::::
<Macro=MyMacro|1>
For /L %a in (1,1,2) Do Echo;Note^^!^^!
Echo;Data code on macro, syntax is bit different
Echo;The '%%a' in for loops change to '%a'.
Echo;
Echo;Does not possible use standar variables, like '%random%'
Echo;neither use macros into a macro.
Echo;
Echo;The '^<Macro=Name^|1^>' cannot include spaces or tabs
Echo;before/after of this.
Echo;
</Macro=MyMacro>
<Macro=MyOtherMacro|2>
Echo;--- %1 %2 %3 %4 %5 %6 %7 %8 %9 ---
</Macro=MyOtherMacro>
::::::::::::::::{ Flush all unused variables }::::::::::::::::
:Flush
For /f "Tokens=1 Delims==" %%a in ('Set') Do (
If /i "%%a" Neq "Comspec" (
If /i "%%a" Neq "Tmp" (
If /i "%%a" Neq "Userprofile" (
IF /i "%%a" Neq "SystemRoot" (
IF /i "%%a" Neq "SystemDrive" (
Set "%%a=")))))
)
Set "Path=%comspec:~0,-8%;%SystemRoot%;%Comspec:~0,-8%\Wbem"
Goto :Eof
:: -> AutoMacro helps you to set variables with functions then be ready to use <- ::
:: -> Type 1: Without Argument pharsing <- ::
:: -> Type 2: With Argument pharsing <- ::
:AutoMacro
Set "Start="
set "Tm=%Tmp%\Tmp.dat"
Set "End="
Set "%~1="
For /F "Tokens=1,3 Delims=>|:" %%a in ('Findstr /N /B "</*Macro=%~1" "%Game%"') Do (
If Not Defined Start (
Set Start=%%a
Set Type=%%b
) Else (
Set End=%%a
)
)
If %Type% Equ 1 (
<Nul Set/p=([LF]>"%Tm%"
)
If %Type% Equ 2 (
<Nul Set/p=For %%` in (0;1^) Do If %%` Equ 1 (For /F "Tokens=1-9" %%1 in ("!Argv!"^) Do ([LF]>"%Tm%"
)
Set/a Start+=2
For /F "Tokens=1* Skip=%Start% Delims=[]" %%a in ('Find /N /V "" "%Game%"') do (
If %%a Geq %End% Goto :End
If "%%b" Neq "" <Nul Set/p "=%%b[LF]">>"%Tm%"
)
:End
If %Type% Equ 1 (
<Nul Set/p=^)>>"%Tm%"
)
If %Type% Equ 2 (
<Nul Set/p=^)^) Else Set Argv=>>"%Tm%"
)
(
For /F "Delims=" %%a in ('Type "%Tm%"') Do Set "%~1=%%a"
) >Nul 2>&1 || Goto :ErrMacro
Del "%Tm%">Nul 2>&1
Set Tm=& Set Start=& Set End=
Goto :Eof
:ErrMacro
Set/a Start-=2
For %%a in (
"Macro Setup Failure from line:"
" Start: '%Start%'"
" End: '%End%'"
" Expected '%~1' macro with size less than 8KB (8100 Letters)"
) Do Echo;%%~a
Pause>Nul
Exit
There are two types of macros:
Type 1:
<Macro=MyMacro|1>
For /L %a in (1,1,2) Do Echo;Note^^!^^!
Echo;Data code on macro, syntax is bit different
Echo;The '%%a' in for loops change to '%a'.
Echo;
Echo;Does not possible use standar variables, like '%random%'
Echo;neither use macros into a macro.
Echo;
Echo;The '^<Macro=Name^|1^>' cannot include spaces or tabs
Echo;before/after of this.
Echo;
</Macro=MyMacro>
Type 2:
<Macro=MyOtherMacro|2>
Echo;--- %1 %2 %3 %4 %5 %6 %7 %8 %9 ---
Echo;Those are my current arguments.
</Macro=MyOtherMacro>
Macro double quotes solution
<Macro=MyOtherMacro|2>
for %d in (
;"Hello"
;"World"
) Do Echo;%~d
Echo;
Echo;You need add semicolons before a double quote
</Macro=MyOtherMacro>
Macro solution is created by Ed Dyreen, Jeb and Dave Benham.
AutoMacro is created by me, Honguito98
If you want more info. about macro system, consider visit at ss64 - macros
Now, if you think can continue, read the Macro Library