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

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

    Use:

    call msbuild.bat
    call unit-tests.bat
    call deploy.bat
    

    When not using CALL, the current batch file stops and the called batch file starts executing. It's a peculiar behavior dating back to the early MS-DOS days.

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

    If you want to open many batch files at once you can use the call command. However, the call command closes the current bat file and goes to another. If you want to open many at once, you may want to try this:

    @echo off
    start cmd "call ex1.bat&ex2.bat&ex3.bat"
    

    And so on or repeat start cmd "call..." for however many files. This works for Windows 7, but I am not sure about other systems.

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

    using "&"

    As you have noticed executing the bat directly without CALL,START, CMD /C causes to enter and execute the first file and then the process to stop as the first file is finished. Though you still can use & which will be the same as using command1 & command2 directly in the console:

    (
        first.bat
    )&(
        second.bat
    )& (
        third.bat
    )&(
        echo other commands
    )
    

    In a term of machine resources this will be the most efficient way though in the last block you want be able to use command line GOTO,SHIFT,SETLOCAL.. and its capabilities will almost the same as in executing commands in the command prompt. And you won't be able to execute other command after the last closing bracket

    Using CALL

    call first.bat
    call second.bat
    call third.bat
    

    In most of the cases it will be best approach - it does not create a separate process but has almost identical behaviour as calling a :label as subroutine. In MS terminology it creates a new "batch file context and pass control to the statement after the specified label. The first time the end of the batch file is encountered (that is, after jumping to the label), control returns to the statement after the call statement."

    You can use variables set in the called files (if they are not set in a SETLOCAL block), you can access directly labels in the called file.

    CMD /C, Pipes ,FOR /F

    Other native option is to use CMD /C (the /C switch will force the called console to exit and return the control) Something that cmd.exe is doing in non transparent way with using FOR /F against bat file or when pipes are used. This will spawn a child process that will have all the environment ot the calling bat. Less efficient in terms of resources but as the process is separate ,parsing crashes or calling an EXIT command will not stop the calling .bat

    @echo off
    CMD /c first.bat
    CMD /C second.bat
    
    ::not so different than the above lines.
    :: MORE,FINDSTR,FIND command will be able to read the piped data 
    :: passed from the left side
    
    break|third.bat
    

    START

    Allows you more flexibility as the capability to start the scripts in separate window , to not wait them to finish, setting a title and so on. By default it starts the .bat and .cmd scripts with CMD /K which means that the spawned scripts will not close automatically.Again passes all the environment to the started scripts and consumes more resources than cmd /c:

    :: will be executed in the same console window and will wait to finish
    start "" /b /w cmd /c first.bat 
    
    ::will start in a separate console window and WONT wait to be finished
    :: the second console window wont close automatically so second.bat might need explicit exit command
    start "" second.bat
    
    ::Will start it in a separate window ,but will wait to finish
    :: closing the second window will cause Y/N prompt
    :: in the original window 
    start "" /w third.cmd
    
    
    ::will start it in the same console window
    :: but wont wait to finish. May lead to a little bit confusing output
    start "" /b cmd /c fourth.bat
    

    WMIC

    Unlike the other methods from now on the examples will use external of the CMD.exe utilities (still available on Windows by default). WMIC utility will create completely separate process so you wont be able directly to wait to finish. Though the best feature of WMIC is that it returns the id of the spawned process:

    :: will create a separate process with cmd.exe /c
    WMIC process call create "%cd%\first.bat","%cd%"
    
    ::you can get the PID and monitoring it with other tools
    for /f "tokens=2 delims=;= " %%# in ('WMIC process call create "%cd%\second.bat"^,"%cd%" ^|find "ProcessId"') do (
        set "PID=%%#"
    )
    echo %PID%
    

    You can also use it to start a process on a remote machine , with different user and so on.

    SCHTASKS

    Using SCHTASKS provides some features as (obvious) scheduling , running as another user (even the system user) , remote machine start and so on. Again starts it in completely separate environment (i.e. its own variables) and even a hidden process, xml file with command parameters and so on :

    SCHTASKS /create /tn BatRunner /tr "%cd%\first.bat" /sc ONCE /sd 01/01/1910 /st 00:00
    SCHTASKS /Run /TN BatRunner
    SCHTASKS /Delete /TN BatRunner /F
    

    Here the PID also can acquired from the event log.

    ScriptRunner

    Offers some timeout between started scripts. Basic transaction capabilities (i.e. rollback on error) and the parameters can be put in a separate XML file.

    ::if the script is not finished after 15 seconds (i.e. ends with pause) it will be killed
    ScriptRunner.exe -appvscript %cd%\first.bat -appvscriptrunnerparameters -wait -timeout=15
    
    
    ::will wait or the first called script before to start the second
    :: if any of the scripts exit with errorcode different than 0 will try
    :: try to restore the system in the original state
    ScriptRunner.exe -appvscript second.cmd arg1 arg2 -appvscriptrunnerparameters -wait -rollbackonerror -appvscript third.bat -appvscriptrunnerparameters -wait -timeout=30 -rollbackonerror
    
    0 讨论(0)
  • 2020-11-22 14:14
    call msbuild.bat
    call unit-tests.bat
    call deploy.bat
    
    0 讨论(0)
  • 2020-11-22 14:16

    If we want to open multiple command prompts then we could use

    start cmd /k
    

    /k: is compulsory which will execute.

    Launching many command prompts can be done as below.

    start cmd /k Call rc_hub.bat 4444
    
    start cmd /k Call rc_grid1.bat 5555
    
    start cmd /k Call rc_grid1.bat 6666
    
    start cmd /k Call rc_grid1.bat 5570.
    
    0 讨论(0)
  • 2020-11-22 14:17

    With correct quoting (this can be tricky sometimes):

    start "" /D "C:\Program Files\ProgramToLaunch" "cmd.exe" "/c call ""C:\Program Files\ProgramToLaunch\programname.bat"""
    

    1st arg - Title (empty in this case)
    2nd arg - /D specifies starting directory, can be ommited if want the current working dir (such as "%~dp0")
    3rd arg - command to launch, "cmd.exe"
    4th arg - arguments to command, with doubled up quotes for the arguments inside it (this is how you escape quotes within quotes in batch)

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