views:

8592

answers:

3

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.

+1  A: 

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.

Moshe
-1 The problem is that START doesn't create a new window when you start a BAT file; instead it reuses the current window and mixes the I/O two CMD processes.
Aaron Digulla
It is not creating a new window because you're using the /B flag. Remove it.
Moshe
A: 

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();
}
Uhm ... I was hoping that something as simple as that wouldn't need installing Visual Studio ...
Aaron Digulla
Until PowerShell is available 100% - C# is really overkill.
jim
Aaron: The C# compiler is included in the .NET Framework 2, so you can just edit some code in a text editor and compile it. Still, since this is perfectly possible in a batch file I'd also say C# is overkill.
Joey
+7  A: 

Actually, the following works fine for me and creates new windows:

test.cmd:

@echo off
call "cmd /c start test2.cmd"
call "cmd /c 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.

Joey
Okay, this works but isn't 100% perfect: After I press "RETURN" in "Test 2", that window doesn't close. Any ideas?
Aaron Digulla
You can add "exit" to the spawned batches to close them afterwards again. Sorry, didn't test that far :)
Joey