In a Windows batch file, can you chain-execute something that is *not* another batch file?

前端 未结 1 1997
后悔当初
后悔当初 2020-12-11 20:40

I understand if you have two .bat or .cmd files, let\'s call them foo and bar, the following rules apply:

Without

相关标签:
1条回答
  • 2020-12-11 21:20

    It is necessary to use command start to run the executable in a separate process and additionally exit current batch processing or entire command process.

    @echo off
    echo Welcome to %~nx0
    start "Title" bar.exe & exit /B
    echo Even though bar is now an .exe, we never get to this line.
    

    This batch file starts bar.exe in a separate process with Title as window title in case of executable is a console application for the new console window opened in this case.

    Then after start finished exit /B is unconditionally executed by command processor while bar.exe is running in a separate process resulting in terminating processing of current batch file.

    If this batch file was not called itself from another batch file using command call, the command processor finishes now processing the batch file resulting in exiting command processing, except the batch file was called with cmd.exe with option /K to keep command prompt window open after finishing batch processing which is not the case by default.

    But if this batch file was called with call from another batch file, just processing of this child batch file is finished and command processor continues processing of parent batch file while bar.exe runs in a separate process.

    @echo off
    echo Welcome to %~nx0
    start "Title" bar.exe & exit
    echo Even though bar is now an .exe, we never get to this line.
    

    In this batch code the command exit is without option /B which results in terminating command processing after start finished starting bar.exe in a separate process even if the current batch file was called from another batch file with call and even if batch file processing was started with cmd.exe with parameter /K.

    Instead of unconditionally concatenating the two commands start and exit with operator & it is also possible to use a block as shown below for the two variants.

    With just exiting current batch processing:

    @echo off
    echo Welcome to %~nx0
    (
        start "Title" bar.exe
        exit /B
    )
    echo Even though bar is now an .exe, we never get to this line.
    

    With exiting entire command process:

    @echo off
    echo Welcome to %~nx0
    (
        start "Title" bar.exe
        exit
    )
    echo Even though bar is now an .exe, we never get to this line.
    

    Such a start of an application with exiting either current batch processing or entire command processing makes of course only sense when bar.exe is started depending on at least one condition in the batch file.

    Note 1:
    It is also possible to use goto :EOF instead of exit /B to end current batch processing.

    Note 2: goto :EOF and exit /B result both in just exiting the subroutine if the command is being part of a batch subroutine, i.e. code below a label called with call :label because a batch subroutine is just like a child batch file embedded within a main batch file regarding batch processing.

    Some more examples to demonstrate the behavior of call and exit /B:

    Test1.bat:

    @echo off
    echo Running %~nx0
    call Test2.bat
    echo Finished %~nx0
    

    Test2.bat:

    @echo off
    echo Running %~nx0
    Test3.bat
    echo Finished %~nx0
    

    Test3.bat:

    @echo off
    echo Finished %~nx0
    

    Running Test1.bat from within a command prompt window results in output:

    Running Test1.bat
    Running Test2.bat
    Finished Test3.bat
    Finished Test1.bat
    

    So the line Finished Test2.bat is missing because command processor returned from Test3.bat directly to Test1.bat.

    Next we compile following C code to console application Test.exe:

    #include <stdio.h>
    
    int main (int argc, char* argv[])
    {
        if(argc > 1)
        {
            printf("Running %s with argument %s\n",argv[0],argv[1]);
        }
        else
        {
            printf("Running %s without an argument\n",argv[0]);
        }
        return 0;
    }
    

    And we make use of Test.exe in following 2 batch files:

    Test4.bat:

    @echo off
    echo Running %~nx0
    Test.exe 4
    call Test5.bat
    echo Finished %~nx0
    

    Test5.bat:

    @echo off
    echo Running %~nx0
    Test.exe 5
    Test.exe 6 & exit /B
    echo Finished %~nx0
    

    Running Test4.bat from within a command prompt window results in output:

    Running Test4.bat
    Running Test.exe with argument 4
    Running Test5.bat
    Running Test.exe with argument 5
    Running Test.exe with argument 6
    Finished Test4.bat
    

    So the line Finished Test5.bat is missing because command processor returned from executing Test.exe with argument 6 directly to Test4.bat.

    But with using bar & exit /B it is nevertheless important if bar is a batch file with file extension bat or cmd, or an executable with file extension exe or com. This can be demonstrated by changing code of Test2.bat to:

    @echo off
    echo Running %~nx0
    Test3.bat & exit /B
    echo Finished %~nx0
    

    Running Test1.bat from within a command prompt window results in output:

    Running Test1.bat
    Running Test2.bat
    Finished Test3.bat
    

    So with exit /B appended in second batch file, command processor interprets exit in second batch file as exit in context of first batch file.

    0 讨论(0)
提交回复
热议问题