Something like a function/method in batch files?
You could use the call command :
call:myDosFunc
And then define the function this way :
:myDosFunc - here starts the function
echo. here the myDosFunc function is executing a group of commands
echo. it could do a lot of things
goto:eof
Source : Batch Functions
Just for completeness, you can also pass parameters to the function:
Function call
call :myDosFunc 100 "string val"
Function body
:myDosFunc
echo. Got Param#1 %~1
echo. Got Param#2 %~2
goto :eof
Placing the reusable functions into a separate batch file would certainly work to simulate a function.
The catch is that you have to use the call
command in order to ensure that control returns to the caller after the second batch file finishes executing.
call 5lines.bat
echo this will now get called
Solution:
@ECHO OFF
call:header Start Some Operation
... put your business logic here
... make sure EXIT below is present
... so you don't run into actual functions without the call
call:header Operation Finished Successfully
EXIT /B %ERRORLEVEL%
:: Functions
:header
ECHO =================================================
ECHO %*
ECHO =================================================
EXIT /B 0
Important to put EXIT /B at the end of each function, as well as before function definitions start, in my example this is:
EXIT /B %ERRORLEVEL%