and*_*eas 13 windows function batch-file
有一种在DOS .bat/.cmd脚本中构建函数的好方法.要模块化一些安装脚本,最好将带有函数库的文件包含到.bat/.cmd脚本中.
我试过的是:
mainscript.bat
call library.bat
call:function1
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
当我调用mainscript.bat然后我得到以下输出:Das Sprungziel - function1 wurde nicht gefunden.
或多或少意味着什么:找不到名为function1的跳转点
任何想法,或者这是不可能的?
jeb*_*jeb 12
这是可能的,并且有一些不同的方法可以做到这一点.
1)将完整的"库"复制并粘贴到每个文件中Works,但它实际上不是库,并且更改/更正所有文件中的库函数是一件恐怖事
2)通过call-wrapper包含一个库
call batchLib.bat :length result "abcdef"
Run Code Online (Sandbox Code Playgroud)
和batchLib.bat以.开头
call %*
exit /b
...
:length
...
Run Code Online (Sandbox Code Playgroud)
易于编程,但速度很慢,因为每个库调用都会加载库批次,以及参数可能出现的问题.
3)"自加载"库BatchLibrary或如何包含批处理文件 (缓存)
它每次创建一个临时批处理文件,结合自己的代码和库代码.
它在库启动时执行一些高级功能,如安全参数访问.但在我看来,它也很容易使用
用户脚本示例
@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%
Run Code Online (Sandbox Code Playgroud)
编辑:另一种方式是......
4)宏库
您可以使用批处理宏,它很容易包含和使用它们.
call MacroLib.bat
set myString=abcdef
%$strLen% result,myString
echo The length of myString is %result%
Run Code Online (Sandbox Code Playgroud)
但是构建宏很棘手!
有关带参数的批处理"宏" 的宏技术的更多信息(缓存)
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=,
Run Code Online (Sandbox Code Playgroud)