Windows .bat/.cmd function library in own file?

后端 未结 6 2037
春和景丽
春和景丽 2021-02-05 14:50

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 .

6条回答
  •  离开以前
    2021-02-05 15:36

    okay... quick & dirty because I'm a UNIX guy... create your "library" file

      1 @ECHO OFF
    2 SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION & PUSHD
    3 :: -----------------------------------------------
    4 :: $Id$
    5 ::
    6 :: NAME:
    7 :: PURPOSE:
    8 :: NOTES:
    9 ::
    10 :: INCLUDES --------------------------------- --
    11 :: DEFINES --------------------------------- --
    12 :: VARIABLES --------------------------------- --
    13 :: MACROS --------------------------------- --
    14
    15 GOTO :MAINLINE
    16
    17 :: FUNCTIONS --------------------------------- --
    18
    19 :HEADER
    20 ECHO ^<HTML^>
    21 ECHO ^<HEAD^>
    22 ECHO ^<TITLE^>%1^</TITLE^>
    23 ECHO ^</HEAD^>
    24 ECHO ^<BODY^>
    25 GOTO :EOF
    26
    27 :TRAILER
    28 ECHO ^</BODY^>
    29 ECHO ^</HTML^>
    30 GOTO :EOF
    31
    32 :: MAINLINE --------------------------------- --
    33 :MAINLINE
    34
    35 IF /I "%1" == "HEADER" CALL :HEADER %2
    36 IF /I "%1" == "TRAILER" CALL :TRAILER
    37
    38 ENDLOCAL & POPD
    39 :: HISTORY ------------------------------------
    40 :: $Log$
    41 :: END OF 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..

      1 @ECHO OFF
    2 SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION & PUSHD
    3 :: -----------------------------------------------
    4 :: $Id$
    5 ::
    6 :: NAME:
    7 :: PURPOSE:
    8 :: NOTES:
    9 ::
    10 :: INCLUDES --------------------------------- --
    11
    12 SET _LIB_=PATH\TO\LIBRARIES\LIBNAME.BAT
    13
    14 :: DEFINES --------------------------------- --
    15 :: VARIABLES --------------------------------- --
    16 :: MACROS --------------------------------- --
    17
    18 GOTO :MAINLINE
    19
    20 :: FUNCTIONS --------------------------------- --
    21 :: MAINLINE --------------------------------- --
    22 :MAINLINE
    23
    24 call %_LIB_% header foo
    25 call %_LIB_% trailer
    26
    27 ENDLOCAL & POPD
    28 :: HISTORY ------------------------------------
    29 :: $Log$
    30 :: END OF FILE --------------------------------- --

    line 12 "imports" the "library"... actually it's just syntactic sugar that makes the subsequent calls easier...

提交回复
热议问题