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

前端 未结 17 1677
别跟我提以往
别跟我提以往 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:25

    Running multiple scripts in one I had the same issue. I kept having it die on the first one not realizing that it was exiting on the first script.

    :: OneScriptToRunThemAll.bat
    CALL ScriptA.bat
    CALL ScriptB.bat
    EXIT
    
    :: ScriptA.bat
    Do Foo
    EXIT
    ::ScriptB.bat
    Do bar
    EXIT
    

    I removed all 11 of my scripts EXIT lines and tried again and all 11 ran in order one at a time in the same command window.

    :: OneScriptToRunThemAll.bat
    CALL ScriptA.bat
    CALL ScriptB.bat
    EXIT
    
    ::ScriptA.bat
    Do Foo
    
    ::ScriptB.bat
    Do bar
    
    0 讨论(0)
  • 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 <batch command>
    

    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.

    0 讨论(0)
  • 2020-11-22 14:28
    Start msbuild.bat
    Start unit-tests.bat
    Start deploy.bat
    

    If that doesn't work, replace start with call or try this:

    Start msbuild.bat
    Goto :1
    :1
    Start unit-tests.bat
    Goto :2
    :2
    Start deploy.bat
    
    0 讨论(0)
  • 2020-11-22 14:28

    I know I am a bit late to the party, but here is another way. That is, this method should wait until the first one is done, the second, and so on.

    start "" /wait cmd.exe /c msbuild.bat
    start "" /wait cmd.exe /c unit-tests.bat
    start "" /wait cmd.exe /c deploy.bat
    

    The only issue that may come out of using this method, is that with new instances of cmd.exe being spawned, is that Errorlevel checking is kept within in each instance of cmd.exe.

    Or..

    start "" /wait call msbuild.bat
    start "" /wait call unit-tests.bat
    start "" /wait call deploy.bat
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 14:30

    Looking at your filenames, have you considered using a build tool like NAnt or Ant (the Java version). You'll get a lot more control than with bat files.

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