How do I run a bat file in the background from another bat file?
Solution 1:
Two years old, but for completeness...
Standard, inline approach: (i.e. behaviour you'd get when using &
in Linux)
START /B CMD /C CALL "foo.bat" [args [...]]
Notes: 1. CALL
is paired with the .bat file because that where it usually goes.. (i.e. This is just an extension to the CMD /C CALL "foo.bat"
form to make it asynchronous. Usually, it's required to correctly get exit codes, but that's a non-issue here.); 2. Double quotes around the .bat file is only needed if the name contains spaces. (The name could be a path in which case there's more likelihood of that.).
If you don't want the output:
START /B CMD /C CALL "foo.bat" [args [...]] >NUL 2>&1
If you want the bat to be run on an independent console: (i.e. another window)
START CMD /C CALL "foo.bat" [args [...]]
If you want the other window to hang around afterwards:
START CMD /K CALL "foo.bat" [args [...]]
Note: This is actually poor form unless you have users that specifically want to use the opened window as a normal console. If you just want the window to stick around in order to see the output, it's better off putting a PAUSE
at the end of the bat file. Or even yet, add ^& PAUSE
after the command line:
START CMD /C CALL "foo.bat" [args [...]] ^& PAUSE
Solution 2:
Actually, the following works fine for me and creates new windows:
test.cmd:
@echo off
start test2.cmd
start test3.cmd
echo Foo
pause
test2.cmd
@echo off
echo Test 2
pause
exit
test3.cmd
@echo off
echo Test 3
pause
exit
Combine that with parameters to start
, such as /min
, as Moshe pointed out if you don't want the new windows to spawn in front of you.