7

I defined a function, the strange thing is that the function lines are executed even if I do NOT call the function !!

echo off
cls
REM call:ctrlService "stop" "1.1.1.1"
echo.SERVICES STARTED
:ctrlService - Generic function
echo.Parameter 1: %~1
echo.Parameter 2: %~2
dir
GOTO:EOF
echo on

I see the echo statements executed, without values displayed for the relative parameters. And the dir command is executed too !

How can I stop the function from being executed on it's own ?

1
  • 1
    The label :ctrlService won't prevent interpreter to skip the following lines (the ones you would call function body). Usually you define your functions at the end of batch file and make the interpreter to goto :eof before it reaches the definitions. Sep 29, 2013 at 15:13

1 Answer 1

11

Adding a label merely adds a label. It does not alter the flow though your batch file.

If you want to skip part of a batch-file then you will have to jump over it.

E.g.

    @echo off
    echo Your batch file has started. Jumping to main.
    GOTO :MAIN

    :function_label
    some_command
    some_command

    :MAIN
    some_command

This will goto to the main part of the batch files, skipping the functions.
Without this goto the function will be reached first, executed and then flow will pass downward.

1
  • 2
    Very interesting !
    – robe007
    Jan 18, 2018 at 21:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .