Run batch files sequentially

北城余情 提交于 2019-11-28 10:58:45
Ishikawa

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!