How do I run a bat file in the background from another bat file?

Windows XpBatch FileCmd

Windows Xp Problem Overview


I have a "setup" script which I run in the morning which starts all the programs that I need. Now some of those need additional setup of the environment, so I need to wrap them in small BAT scripts.

How do I run such a script on Windows XP in the background?

CALL env-script.bat runs it synchronously, i.e. the setup script can continue only after the command in the env-script has terminated.

START/B env-script.bat runs another instance of CMD.exe in the same command prompt, leaving it in a really messy state (I see the output of the nested CMD.exe, keyboard is dead for a while, script is not executed).

START/B CMD env-script.bat yields the same result. None of the flags in CMD seem to match my bill.

Windows Xp Solutions


Solution 1 - Windows Xp

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 - Windows Xp

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.

Solution 3 - Windows Xp

Since START is the only way to execute something in the background from a CMD script, I would recommend you keep using it. Instead of the /B modifier, try /MIN so the newly created window won't bother you. Also, you can set the priority to something lower with /LOW or /BELOWNORMAL, which should improve your system responsiveness.

Solution 4 - Windows Xp

Other than foreground/background term. Another way to hide running window is via vbscript, if is is still available in your system.

DIM objShell
set objShell=wscript.createObject("wscript.shell")
iReturn=objShell.Run("yourcommand.exe", 0, TRUE)

name it as sth.vbs and call it from bat, put in sheduled task, etc. PersonallyI'll disable vbs with no haste at any Windows system I manage :)

Solution 5 - Windows Xp

Actually is quite easy with this option at the end:

> c:\start BATCH.bat -WindowStyle Hidden

Solution 6 - Windows Xp

Create a new C# Windows application and call this method from main:

public static void RunBatchFile(string filename)
{
	Process process = new Process();

	process.StartInfo.FileName = filename;
	
	// suppress output (command window still gets created)
	process.StartInfo.Arguments = "> NULL";

	process.Start();
	process.WaitForExit();
}

Solution 7 - Windows Xp

This works on my Windows XP Home installation, the Unix way:

call notepad.exe & 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAaron DigullaView Question on Stackoverflow
Solution 1 - Windows XpantakView Answer on Stackoverflow
Solution 2 - Windows XpJoeyView Answer on Stackoverflow
Solution 3 - Windows XpMosheView Answer on Stackoverflow
Solution 4 - Windows XpMeaCulpaView Answer on Stackoverflow
Solution 5 - Windows Xpuser8832474View Answer on Stackoverflow
Solution 6 - Windows XpLegendLengthView Answer on Stackoverflow
Solution 7 - Windows XpMiiTooView Answer on Stackoverflow