Run batch files sequentially

后端 未结 3 525
清歌不尽
清歌不尽 2020-12-10 13:24

I want to ask you all how to run batch files sequentially in Windows. I have tried :

start /w batchfile_1.bat 
start /w batchfile_2.bat
..
start /w batchfile         


        
相关标签:
3条回答
  • 2020-12-10 13:53

    I would check the solutions to this question: Run Multiple batch files

    • Taken from the answer in the link.

    Use call:

    call bat1.cmd
    call bat2.cmd
    

    By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.

    Basically, if you have a batch like this:

    @echo off
    echo Foo
    batch2.cmd
    echo Bar
    

    then it will only output

    Foo
    

    If you write it like

    @echo off
    echo Foo
    call batch2.cmd
    echo Bar
    

    however, it will output

    Foo
    Bar
    

    because after batch2 terminates, program control is passed back to your original batch file.

    0 讨论(0)
  • 2020-12-10 14:00

    If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.

    @echo off
     .
     .
    :: Inspired coding
     .
     .
    exit
    
    0 讨论(0)
  • 2020-12-10 14:02

    I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:

    1. A START /W command is invoked and starts a batch file.

    2. The batch file starts executing and runs a program.

    3. The batch file finishes and its console window remains open, but the program continues running.

    4. The START /W command that was used to run the batch file is still executing because the console window remains open.

    5. You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.

    Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.

    I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.

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