Skip to content

Latest commit

 

History

History
executable file
·
158 lines (116 loc) · 3.25 KB

File metadata and controls

executable file
·
158 lines (116 loc) · 3.25 KB
  1. ✅ Use Subroutines (Labels + CALL)

    The most straightforward way to create reusable code in a batch file is using a subroutine.

    @ECHO OFF
    CALL :PrintHeader "Disk Check Utility"
    CALL :PrintHeader "Log Cleaner"
    
    GOTO :EOF
    
    :PrintHeader
    ECHO.
    ECHO ============================
    ECHO      %1
    ECHO ============================
    ECHO.
    EXIT /B

    🔍 How This Is Reusable:

    • :PrintHeader can be reused to print a formatted title anywhere.
    • %1 allows passing in a dynamic value.
  2. 📂 Organize Subroutines into Utility Sections

    You can build modular utility libraries at the bottom of your script.

    @ECHO OFF
    CALL :LogInfo "Script started"
    REM Your main logic here...
    CALL :LogInfo "Script ended"
    GOTO :EOF
    
    :: -------------------------------
    :: Logging Utilities
    :: -------------------------------
    :LogInfo
    ECHO [INFO] %~1
    EXIT /B
    
    :LogError
    ECHO [ERROR] %~1
    EXIT /B

    💡 Use :: or REM to group utility sections clearly.

  3. 📌 External Utility Script (Reusable Across Files)

    You can create reusable .bat libraries by keeping utility subroutines in a separate file and calling it from multiple scripts.

    📁 utils.bat (only utility functions)

    @ECHO OFF
    GOTO :EOF
    
    :LogInfo
    ECHO [INFO] %~1
    EXIT /B
    
    :LogError
    ECHO [ERROR] %~1
    EXIT /B

    📁 main.bat

    @ECHO OFF
    CALL utils.bat
    CALL :LogInfo "Started main script"
    REM More logic
    CALL :LogError "Something went wrong"
    GOTO :EOF

    ⚠️ You must CALL the file and make sure it doesn't execute its own labels unless specifically called.

  4. 🧪 Example: Reusable Math Operations

    @ECHO OFF
    CALL :Add 15 25 result
    ECHO Sum is: %result%
    
    CALL :Multiply 5 10 result
    ECHO Product is: %result%
    
    GOTO :EOF
    
    :Add
    SETLOCAL
    SET /A temp=%1 + %2
    ENDLOCAL & SET %3=%temp%
    EXIT /B
    
    :Multiply
    SETLOCAL
    SET /A temp=%1 * %2
    ENDLOCAL & SET %3=%temp%
    EXIT /B

    This pattern simulates returning a value via a third parameter.

  • 🧱 Simulating Functions with Multiple Outputs

    Need multiple outputs? You’ll have to use multiple variable names.

    @ECHO OFF
    CALL :DivideAndRemainder 10 3 result remainder
    ECHO Quotient: %result%
    ECHO Remainder: %remainder%
    GOTO :EOF
    
    :DivideAndRemainder
    SETLOCAL
    SET /A q=%1 / %2
    SET /A r=%1 %% %2
    ENDLOCAL & (
        SET %3=%q%
        SET %4=%r%
    )
    EXIT /B
  • ⚙️ Advanced: Use Dynamic Labels (Function Dispatcher)

    If you want a more abstract way to call code blocks dynamically:

    @ECHO OFF
    SET task=LogStart
    CALL :%task%
    
    SET task=LogEnd
    CALL :%task%
    
    GOTO :EOF
    
    :LogStart
    ECHO Starting process...
    EXIT /B
    
    :LogEnd
    ECHO Process complete.
    EXIT /B

    Useful for command dispatchers or menu-based systems.