Batch file how to call another batch file at a specified label or call and immediately goto a certain label?

前端 未结 5 1392
不知归路
不知归路 2021-01-02 00:57

I am trying to figure out how file1.bat can call file2.bat at a specified label.

I figured I can do it like this:

File1.bat

:config
         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 01:46

    You can use a strange trick!
    You can goto a label in a secondary batch without calling it in the secondary batch!

    First.bat

    @echo off
    call :label
    echo returned
    exit /b
    
    :label
    second.bat
    exit /b
    

    Second.bat

    @echo off
    echo Main of second.bat
    exit /b
    
    :label
    echo This is second.bat at LABEL
    exit /b
    

    OUTPUT

    This is second.bat at LABEL
    returned
    

    There seems to be no cause why the label is called, nor why the control should return to the first.bat, as second batch was called without a CALL.
    The cause for the first point seems to be the internal code of the goto command.
    The second point can be explained, as there is one call prior to the dummy label in the first batch file.
    The exit /b in second.bat returns directly to the call (line 3) of first.bat not to the invocation of second.bat at line 7

    EDIT: How to disable the odd behaviour

    if you append a command at second.bat it will no longer implicit jump to the label in second.bat.

    second.bat & rem will change the output to OUTPUT

    Main of second.bat
    returned
    

提交回复
热议问题