Run batch files sequentially - batch-file

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_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.

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:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
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.

Related

Run a different batch file when one coses

I have 2 batch files (1.bat and 2.bat). I want 2.bat to run when 1.bat closes. Is that possible?
Or is there a way to close a batch file when a batch file closes. Here is my code which doesnt work:
cd C:\xampp
start apache_start.bat
cd C:\Users\MinecraftServer\Desktop\1.12.2MinecraftServer
MC1.12.2Start.bat
taskkill /F /IM cmd.exe /T
I have a minecraft server running with a .bat file and a web server running off of a .bat file (using xampp, its used for prtg monitorng).
start will create a completely independent process and continue immediately to your next batch command. That independent process may do a job and terminate, or may say open an application like notepad and wait until the notepad is closed. Regardless, a straight start command will not stop the batch that it is in - the next step will be executed, whether or not the process that has been started has terminated.
You can also add the /wait switch to start. If this switch is used, the batch will wait until the started process terminates.
Preferred syntax for start is
start /wait "window title" executable parameters...
where "window title" may be an empty, but not absent string. Including this as the first parameter is preferred because the first quoted string in a start command is used as a window title for the started process, and thus start may not act as expected where the executable or any of its parameters is quoted.
call executablename... will wait for that executable to terminate before progressing to the next step. This may or may not be what is desired.
MC1.12.2Start.bat in your code will SWITCH to that batch and remaining batch lines will be ignored.
Which may be fortunate in this case, since as you've coded it, the apache_start.bat may or may not have completed when the taskkill command is run, killing all cmd.exe sessions.

Start multiple JavaScript files and wait for them to finish

I am building a program that outputs a batch file. In this batch file there are some links to JavaScript files, which are then supposed to run within photoshop.
Most of it works, but I just want the next JavaScript to run when the previous one is finished! Is this possible?
Yes, this is certainly possible using start with the /wait flag, telling the system to wait to finish a command before processing the next command. Run each file with:
start "" /wait node something.js [arguments]
It is suggested for you, to read the output of start /? carefully in cmd.exe.

Running batch file, call another batch, ctrl+c out of that, then return to the first?

I have a batch script which mount's a .vhd file and creates a junction point, the it call's another batch script which runs some things on the mounted vhd/junction point. When that script finishes, it then comes back to the 1st batch script and deletes junction point, unmounts .vhd then exits.
The problem is the second script needs to exit properly in order to run the rest of the first script, when most of the users will just ctrl+c and close the second script.
Is it possible to prevent this from happening, either by not allowing the ctrl+c cancel or somehow restart the first batch file after the call has happened?
try this to start the 2nd batch file:
start /b /w "" 2nd-batch.bat
Thanks to Endoro's answer, even though it didn't work for me, it made me remember start, i always forget start and just use call. anyway the solution i found was was to do this:
2>nul (
echo N|start /wait "" cmd /c C:\2nd-batch.BAT
)
it creates a new window for the 2nd-batch.bat which allows the dumbuser to even exit it via the X button and it still completes the rest of the 1st-batch.bat

How to create a .bat file that starts a customized CMD, in which it run a script?

I want to make a automated program in .bat. The program needs to run a command. However, the command must be run from a custom CMD.
If I open a regular CMD, the commands that I will do:
C:\Hardware\bin\StartCustomCMD.bat init (This is the first thing I will type. It starts the custom CMD.)
bb autobuild (This is the second thing that I will type. The command goes into the custom CMD)
You can probably tell that I did not write these scripts. I am trying to set this up in Windows Scheduler so that the script gets run automatically every day. Any help on how can I do that?
Thanks.
Make yourself a new batch file, and embed the other things into it, and then run that.
#echo off
call C:\Hardware\bin\StartCustomCMD.bat
bb autobuild
If bb it itself a batch file, then use call on it also. What call does is execute another batch file, and then continue processing. If you don't use call, when you run one batch file from another, the latter 'takes over' and the caller does not continue.
To do this you can use the timeout and goto command. Timeout waits a period of time in seconds but it can be skipped by pressing any key while cmd is open at the top layer. If you can see cmd press its icon on the desktop until you can not see it. Then using the goto command you can go to the top line. So here would be your script:
:start
C:\Hardware\bin\StartCustomCMD.bat init
bb autobuild
timeout 86400
goto start
You already know what the first two commands do, but timeout 86400 waits exactly one day then the goto start command goes to the first command so that gets repeated. If you need to add any more commands then put them above the timeout 86400 command.

Batch script stops after first call to other batch script

I'm attempting to execute a batch script that currently looks like this:
D:
cd D:My Documents\FtpSolution\Test
getftp.bat
call delimconvert.exe
call convert-to-xls.bat
However this stops dead after getftp.bat has run.
What am I doing wrong? It's important that these commands all run sequentially.
Use call:
Calls one batch program from another.
CALL [drive:][path]filename [batch-parameters]
batch-parameters Specifies any command-line information required by the
batch program.
If you invoke other batch files without call then control is passed to them but not back again (which is what call changes).
use start command to launch it in a new window.
start /wait getftp.bat
Try using "Goto :EOF" rather than "exit" at the end of the batch file that you're calling - in your case, the getftp.bat file...
That's what fixed mine - tested on Win10 enterprise.

Resources