在自己的文件中的Windows的.bat / .CMD函数库? [英] Windows .bat/.cmd function library in own file?
问题描述
有一个很好的方式来构建功能DOS的.bat / .CMD脚本的。模块化一些安装脚本,它会很高兴以包括一个函数库文件到蝙蝠/ .CMD脚本。
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.
我的尝试是:
mainscript.bat
mainscript.bat
call library.bat
call:function1
library.bat
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
当我打电话mainscript.bat然后我得到以下的输出:
达斯Sprungziel - 功能1 wurde nicht gefunden
When I call mainscript.bat then I get following output: Das Sprungziel - function1 wurde nicht gefunden.
拿什么或多或少:找不到指定跳点功能1
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)复制和放大器;粘贴完整的库您的每一个文件
作品,但它不是一个真正的图书馆,它的改变/正确的所有文件中的库函数恐怖
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)通过包括呼叫包装库
2) include a library via call-wrapper
call batchLib.bat :length result "abcdef"
和batchLib.bat以
and batchLib.bat starts with
call %*
exit /b
...
:length
...
容易的方案,但很慢,因为每个库调用加载库批次,并用参数可能出现的问题。
Easy to program, but very slow, as each library call loads the library batch, and possible problems with the parameters.
3)自动装弹库 BatchLibrary或如何包括批处理文件
它创建每次一个临时批处理文件,并结合自己的code和库code的。结果
它在库启动类似的安全参数的访问做一些高级功能。
但在我看来,这也很容易使用。
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
一个用户脚本示例
@echo off
REM 1. Prepare the BatchLibrary for the start command
call BatchLib.bat
REM 2. Start of the Batchlib, acquisition of the command line parameters, activates the code with the base-library
<:%BL.Start%
rem Importing more libraries ...
call :bl.import "bl_DateTime.bat"
call :bl.import "bl_String.bat"
rem Use library functions
call :bl.String.Length result abcdefghij
echo len=%result%
编辑:另一种方法是...
4)的宏库
您可以使用批量宏,可以很容易地包含和使用它们。
You could use batch-macros, it's easy to include and to use them.
call MacroLib.bat
set myString=abcdef
%$strLen% result,myString
echo The length of myString is %result%
但它的棘手打造的宏!结果
详细了解批宏与参数的宏观工艺
的 MacroLibrary.bat 的
set LF=^
::Above 2 blank lines are required - do not remove
set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
:::: StrLen pString pResult
set $strLen=for /L %%n in (1 1 2) do if %%n==2 (%\n%
for /F "tokens=1,2 delims=, " %%1 in ("!argv!") do (%\n%
set "str=A!%%~2!"%\n%
set "len=0"%\n%
for /l %%A in (12,-1,0) do (%\n%
set /a "len|=1<<%%A"%\n%
for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"%\n%
)%\n%
for %%v in (!len!) do endlocal^&if "%%~b" neq "" (set "%%~1=%%v") else echo %%v%\n%
) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=,
这篇关于在自己的文件中的Windows的.bat / .CMD函数库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!