How to run multiple .BAT files within a .BAT file

前端 未结 17 1920
别跟我提以往
别跟我提以往 2020-11-22 13:59

I\'m trying to get my commit-build.bat to execute other .BAT files as part of our build process.

Content of commit-build.bat:



        
17条回答
  •  长情又很酷
    2020-11-22 14:26

    All the other answers are correct: use call. For example:

     call "msbuild.bat"
    

    History

    In ancient DOS versions it was not possible to recursively execute batch files. Then the call command was introduced that called another cmd shell to execute the batch file and returned execution back to the calling cmd shell when finished.

    Obviously in later versions no other cmd shell was necessary anymore.

    In the early days many batch files depended on the fact that calling a batch file would not return to the calling batch file. Changing that behaviour without additional syntax would have broken many systems like batch menu systems (using batch files for menu structures).

    As in many cases with Microsoft, backward compatibility therefore is the reason for this behaviour.

    Tips

    If your batch files have spaces in their names, use quotes around the name:

    call "unit tests.bat"
    

    By the way: if you do not have all the names of the batch files, you could also use for to do this (it does not guarantee the correct order of batch file calls; it follows the order of the file system):

    FOR %x IN (*.bat) DO call "%x"
    

    You can also react on errorlevels after a call. Use:

    exit /B 1   # Or any other integer value in 0..255
    

    to give back an errorlevel. 0 denotes correct execution. In the calling batch file you can react using

    if errorlevel neq 0 
    

    Use if errorlevel 1 if you have an older Windows than NT4/2000/XP to catch all errorlevels 1 and greater.

    To control the flow of a batch file, there is goto :-(

    if errorlevel 2 goto label2
    if errorlevel 1 goto label1
    ...
    :label1
    ...
    :label2
    ...
    

    As others pointed out: have a look at build systems to replace batch files.

提交回复
热议问题