there is a nice way to build functions in DOS .bat/.cmd script. To modularize some installation scripts, it would be nice to include a file with a library of functions into an .bat/.cmd script.
what I tried was:
mainscript.bat
call library.bat
call:function1
library.bat
goto:eof
:stopCalipri -- stop alle prozesse die mit calipri zu tun haben
:: -- %~1: argument description here
SETLOCAL
REM.--function body here
set LocalVar1=dummy
set LocalVar2=dummy
echo "Called function successfully :)"
(ENDLOCAL & REM -- RETURN VALUES
IF "%~1" NEQ "" SET %~1=%LocalVar1%
IF "%~2" NEQ "" SET %~2=%LocalVar2%
)
GOTO:EOF
When I call mainscript.bat then I get following output: Das Sprungziel - function1 wurde nicht gefunden.
What means more or less: Can not find jump point named function1
Any ideas, or is this not possible?
It's possible, and there are some different ways to do it.
1) Copy&Paste the complete "Library" into each of your files Works, but it's not really a library, and it's a horror to change/correct a library function in all files
2) include a library via call-wrapper
and batchLib.bat starts with
Easy to program, but very slow, as each library call loads the library batch, and possible problems with the parameters.
3) A "self-loading" library BatchLibrary or how to include batch files (cached)
It creates each time a temporary batch file, combined of the own code and the library code.
It do some advanced functions at the library startup like secure parameter access. But in my opinion it's also easy to use
A user script sample
EDIT: Another way is ...
4) A macro library
You could use batch-macros, it's easy to include and to use them.
But it's tricky to build the macros!
More about the macro technic at Batch "macros" with arguments (cached)
MacroLibrary.bat
I came up with a simple solution for using external libraries with batch files, and I would like to ask you guys to test it and find possible bugs.
How to use:
Principle of operation:
How it works:
Advantages:
Code:
Your_batch_file.bat
.
Example.bat
If you want an easy way to set the %BatchLibraryPath%, just put this file inside your Library path and run it before running Your_batch_file.bat. This setting is persistent on reboots, so you run this once only:
SetBatchLibraryPath.bat
I wrote a script to import subroutines into the main script.
The syntax is something like:
Please see this answer.
okay... quick & dirty because I'm a UNIX guy... create your "library" file
this should be pretty straight-forward for you... at line 15 we make the jump to mainline to begin actual execution. At lines 19 and 27 we create entry points for our routines. At lines 35 and 36 are calls to the internal routines.
now, you build the file that will call the library routines..
line 12 "imports" the "library"... actually it's just syntactic sugar that makes the subsequent calls easier...
Another solution would be to temporary append the library functions to the running batch file.
The original file can be saved in a temporary file before the change and restored when finished. This has the downside that you need to call the :deimport function at the end to restore the file and remove the temporary file. You also need to be able to write to the batch file and the folder you are currently in.
demo.bat
test.bat
C:\path with spaces\lib 2.bat
Alternatively using the external version. Note this imports the deimport function so make sure you remove it in the demo.bat file.
import.bat
There is an easier way to load the library functions each time the main file is executed. For example: