Say, if I have
- foo.exe
- bar.exe
- baz.exe
How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop?
Say, if I have
How do I run all of them from a batch file asynchronously, i.e. without waiting for the previous program to stop?
There's a third (and potentially much easier) option. If you want to spin up multiple instances of a single program, using a Unix-style command processor like Xargs or GNU Parallel can make that a fairly straightforward process.
There's a win32 Xargs clone called PPX2 that makes this fairly straightforward.
For instance, if you wanted to transcode a directory of video files, you could run the command:
Picking this apart,
dir /b *.mpg
grabs a list of .mpg files in my current directory, the|
operator pipes this list into ppx2, which then builds a series of commands to be executed in parallel; 4 at a time, as specified here by the-P 4
operator. The-L 1
operator tells ppx2 to only send one line of our directory listing to ffmpeg at a time.After that, you just write your command line (
ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"
), and{}
gets automatically substituted for each line of your directory listing.It's not universally applicable to every case, but is a whole lot easier than using the batch file workarounds detailed above. Of course, if you're not dealing with a list of files, you could also pipe the contents of a textfile or any other program into the input of pxx2.
I could not get anything to work I ended up just using powershell to start bat scripts .. sometimes even start cmd /c does not work not sure why .. I even tried stuff like start cmd /c notepad & exit
Combining a couple of the previous answers, you could try
start /b cmd /c foo.exe
.For a trivial example, if you wanted to print out the versions of java/groovy/grails/gradle, you could do this in a batch file:
If you have something like Process Explorer (Sysinternals), you will see a few child cmd.exe processes each with a java process (as per the above commands). The output will print to the screen in whatever order they finish.
Using the
START
command to run each program should get you what you need:Every
START
invocation runs the command given in its parameter and returns immediately, unless executed with a/WAIT
switch.That applies to command-line apps. Apps without command line return immediately anyway, so to be sure, if you want to run all asynchronously, use
START
.You can use the start command to spawn background processes without launching new windows:
start /b foo.exe
The new process will not be interruptable with CTRL-C; you can kill it only with CTRL-BREAK (or by closing the window, or via Task Manager.)
Use the START command:
If the path to the program contains spaces remember to add quotes. In this case you also need to provide a title for the opening console window
If you need to provide arguments append them at the end (outside the command quotes)
Use the /b option to avoid opening a new console window (but in that case you cannot interrupt the application using CTRL-C