214

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RichN
  • 6,181
  • 3
  • 30
  • 38

7 Answers7

286

Using the START command to run each program should get you what you need:

START "title" [/D path] [options] "command" [parameters]

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
macbirdie
  • 16,086
  • 6
  • 47
  • 54
  • 1
    Okay, thanks. I should have read each entry of `help` carefully. :) – RichN Sep 19 '09 at 18:44
  • 3
    Shameless self-advertising: I once created a batch which is able to function as some kind of thread pool, just for processes: http://stackoverflow.com/questions/672719/parallel-execution-of-shell-processes/676667#676667 – Joey Sep 19 '09 at 20:50
  • 5
    If you need to provide arguments or if the path to the commands contains spaces remember to add quotes. In this case you also need to provide a title for the opening console window: start "" "[path to command]" [command args] – Pierluigi Jul 09 '14 at 10:08
  • *"Apps without command line return immediately anyway"* – Are you sure? [Why GUI application blocks a batch file?](https://stackoverflow.com/q/19381082/850848) – Martin Prikryl Jul 26 '21 at 11:01
75

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:

@start /b cmd /c java -version
@start /b cmd /c gradle -version
@start /b cmd /c groovy -version
@start /b cmd /c grails -version

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.

start /b :  Start application without creating a new window. The
             application has ^C handling ignored. Unless the application
             enables ^C processing, ^Break is the only way to interrupt
             the application

cmd /c : Carries out the command specified by string and then terminates
djKianoosh
  • 989
  • 9
  • 13
  • 14
    it prevents the command from being echoed to the console. In batch scripts, sometimes you'll see `echo off` at the beginning which, when you execute the script, will prevent all the commands from being echoed to the console. The `@` is similar but just for that single command. So, sometimes you'll see `@echo off`. – djKianoosh Feb 29 '16 at 14:09
  • Did not work for me. Still stopped waiting for the application to close before it moved on. – Chizl Sep 28 '20 at 01:21
39

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.)

sproaticus
  • 509
  • 3
  • 3
  • 7
    Where is `BREAK` on keyboards these days? – user2023370 Jan 10 '16 at 17:07
  • 1
    @user2023370 Still on the [Pause](https://en.wikipedia.org/wiki/File:ANSI_Keyboard_Layout_Diagram_with_Form_Factor.svg) key. Most laptops that lack certain keys support [hidden key combinations](https://support.lenovo.com/at/en/documents/ht074004). – Gerold Broser May 08 '16 at 12:47
  • 1
    @user2023370 [Pause/Break key on modern keyboards](http://superuser.com/q/12525/241386) https://en.wikipedia.org/wiki/Break_key#Modern_keyboards – phuclv Mar 01 '17 at 05:42
  • Did not work for me. Still stopped waiting for the application to close before it moved on. – Chizl Sep 28 '20 at 01:21
26

Create a batch file with the following lines:

start foo.exe
start bar.exe
start baz.exe 

The start command runs your command in a new window, so all 3 commands would run asynchronously.

Nikhil
  • 2,028
  • 7
  • 24
  • 33
  • 1
    works as long as you don't have a path with spaces.. Then when you put double quotes around it, it thinks in a title, not an application. This works. START "VMPlayer" /D "C:\Users\gavin\Virtual Machines\CentOS 8 64-bit" "C:\Program Files (x86)\VMware\VMware Player\vmplayer.exe" – Chizl Sep 28 '20 at 01:25
19

Use the START command:

start [programPath]

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

start "[title]" "[program path]"

If you need to provide arguments append them at the end (outside the command quotes)

start "[title]" "[program path]" [list of command args]

Use the /b option to avoid opening a new console window (but in that case you cannot interrupt the application using CTRL-C

Salvioner
  • 303
  • 5
  • 16
Pierluigi
  • 2,212
  • 1
  • 25
  • 39
11

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:

dir /b *.mpg |ppx2 -P 4 -I {} -L 1 ffmpeg.exe -i "{}" -quality:v 1 "{}.mp4"

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.

schmod
  • 777
  • 9
  • 19
  • 1
    The latest xargs win32 port works well nowadays (the one included with git for windows anyway). – Peter M Nov 13 '14 at 22:11
  • This is great except it fails dealing with files containing unicode characters. Any plans to support unicode files? Thanks. – Gökhan Sever Nov 18 '14 at 15:09
  • @GökhanSever In the past I've worked around this issue by using the "short name" form of the path, that looks like C:\PROGRA~1\WINDOW~3\MYPROG~1.EXE. Not pretty, but it works. – moltenform Jan 07 '17 at 20:12
0

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

start-Process "c:\BACKUP\PRIVATE\MobaXterm_Portable\MobaXterm_Portable.bat" -WindowStyle Hidden
RmccurdyDOTcom
  • 173
  • 1
  • 7