Page 1 of 1
how to include a batch file as module
Posted: 07 Apr 2019 19:21
by sincos2007
Code: Select all
::module1.bat
echo loading module...
:module_function1
echo This is function1 in module
goto :eof
echo end loading
My question is:
how to include module1.bat as library and use function in the module from a batch file, just like ‘use’ statement in Perl
Thanks.
Re: how to include a batch file as module
Posted: 07 Apr 2019 20:47
by dbenham
There is no exact corollary. But below is an interesting strategy. I can't remember who first posted this on DosTips.
If you CALL a :subroutine, and that routine invokes another batch file, then the 2nd batch file automatically branches to a :subroutine with the same name. The 2nd batch returns immediately without error if the routine is not found. Because the parent batch has an active CALL, the second batch will return to the parent when finished. The only downside I'm aware of is the %0 value gives the name of the 2nd bat instead of the name of the CALLed routine.
test.bat
Code: Select all
@echo off
setlocal
call :add 6 3
call :subtract 6 3
call :multiply 6 3
call :divide 6 3
echo(
set "str=The quick brown fox jumps over a lazy dog"
echo Print length of "%str%"
call :strlen str
echo Compute length of "%str%" and store in val
call :strlen str val
echo val=%val%
exit /b
:: Add a label for each function within module.bat you want to include
:add
:subtract
:multiply
:divide
:strlen
module.bat %*
module.bat
Code: Select all
exit /b
:add
set /a result=%1 + %2
echo %1 + %2 = %result%
exit /b
:subtract
set /a result=%1 - %2
echo %1 - %2 = %result%
exit /b
:multiply
set /a result=%1 * %2
echo %1 * %2 = %result%
exit /b
:divide
set /a result=%1 / %2
echo %1 / %2 = %result%
exit /b
:strLen StrVar [RtnVar]
::
:: Computes the length of string inn StrVar
::
:: Sets RtnVar=result
:: or displays result if rtnVar not specified
::
setlocal EnableDelayedExpansion
set "s=!%~1!#"
set "len=0"
for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%P,1!" NEQ "" (
set /a "len+=%%P"
set "s=!s:~%%P!"
)
)
if "%~2" equ "" (echo %len%) else (
endlocal
set "%~2=%len%"
)
exit /b
--RESULT of test.bat--
Code: Select all
6 + 3 = 9
6 - 3 = 3
6 * 3 = 18
6 / 3 = 2
Print length of "The quick brown fox jumps over a lazy dog"
41
Compute length of "The quick brown fox jumps over a lazy dog" and store in val
val=41
Dave Benham
Re: how to include a batch file as module
Posted: 08 Apr 2019 04:14
by sincos2007
Hi Benham,
Your technics really work.
Thanks.
Re: how to include a batch file as module
Posted: 08 Apr 2019 06:06
by dbenham
Perhaps the simplest approach is to abandon the goal of putting multiple routines in one module.bat. Instead you can create a batFunctions folder to house a separate bat file for each routine. Then you simply need to make sure the batFunctions path is in in your PATH. Any batch script can then simply CALL any function within that folder (without the colon prefix of course).
You could configure your machine to always have batFunctions in your PATH.
An alternative that would be very similar to an #include directive would be to have your parent batch script modify PATH (after a SETLOCAL) and put the batFunctions path in the front.
someScript.bat
Code: Select all
@echo off
setlocal
path c:\batFunctions;%path%
...
It is possible for the above to fail if your PATH includes any unquoted poison characters (unlikely, but possible). Switching to delayed expansion should eliminate that problem.
Code: Select all
@echo off
setlocal enableDelayedExpansion
path c:\batFunctions;!path!
...
You don't want to forget the SETLOCAL, else your PATH will grow each time you run your script. The SETLOCAL makes it more difficult to have a batch script define environment variables that survive the ENDLOCAL. But there is loads of information on DosTips describing techniques to preserve values after ENDLOCAL.
Dave Benham