Something like a function/method in batch files?

后端 未结 9 450
忘了有多久
忘了有多久 2020-12-08 00:05

is there anything that mimicks a method like one knows it from Java, C# etc.? I have 5 lines of commands in a batch file, those 5 lines are used at more than one place insid

9条回答
  •  盖世英雄少女心
    2020-12-08 00:36

    Solution:

    @ECHO OFF     
    
    call:header Start Some Operation
    
    ... put your business logic here
    ... make sure EXIT below is present
    ... so you don't run into actual functions without the call
    
    call:header Operation Finished Successfully
    
    EXIT /B %ERRORLEVEL%
    
    :: Functions
    
    :header
    ECHO ================================================= 
    ECHO %*
    ECHO ================================================= 
    EXIT /B 0
    

    Important to put EXIT /B at the end of each function, as well as before function definitions start, in my example this is:

    EXIT /B %ERRORLEVEL%

提交回复
热议问题