如何在另一个bat文件的背景下运行bat文件?

时间:2022-09-02 16:36:14

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.

我有一个“设置”脚本,我在早上运行它启动所有我需要的程序。现在有些需要额外的环境设置,所以我需要用小的BAT脚本包装它们。

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

我如何在后台运行Windows XP上的脚本?

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

env-script打电话。bat可以同步运行,也就是说,只有在env脚本中的命令终止之后,安装脚本才能继续。

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

/ B env-script开始。bat运行另一个CMD实例。exe在同一个命令提示符中,使它处于一个非常混乱的状态(我看到了嵌套CMD的输出)。exe,键盘已经死了一段时间,脚本没有执行)。

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

/ B CMD env-script开始。蝙蝠产生同样的结果。CMD里的国旗似乎都不符合我的要求。

7 个解决方案

#1


65  

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

实际上,下面的工作对我来说很好,并创建了新的窗口:

test.cmd:

test.cmd:

@echo off
call "cmd /c start test2.cmd"
call "cmd /c start test3.cmd"
echo Foo
pause

test2.cmd

test2.cmd

@echo off
echo Test 2
pause
exit

test3.cmd

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.

将其与参数结合起来,例如/min,如Moshe指出的,如果您不想让新窗口在您的前面生成。

#2


122  

Two years old, but for completeness...

两岁,但为了完整…

Standard, inline approach: (i.e. behaviour you'd get when using & in Linux)

标准的内联方法(即在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.).

注:1。调用与.bat文件配对,因为它通常在那里。(例如,这只是CMD /C调用的扩展名)。bat”表单让它异步。通常,需要正确地获取退出代码,但这不是问题所在。2。只有在名称包含空格时,才需要双引号。(这个名字可能是一条路径,在这种情况下,可能性更大。)

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)

如果您希望bat在独立的控制台运行:(即另一个窗口)

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:

注意:这实际上是一个糟糕的表单,除非您的用户特别想使用打开的窗口作为一个正常的控制台。如果你只是想让窗口停留在屏幕上以查看输出,最好在bat文件的末尾暂停一下。甚至,之后添加^ &暂停命令行:

START CMD /C CALL "foo.bat" [args [...]] ^& PAUSE

#3


5  

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.

因为START是在CMD脚本的背景下执行某些事情的唯一方法,所以我建议您继续使用它。不要使用/B修改器,尝试/MIN,这样新创建的窗口不会打扰到你。此外,您可以将优先级设置为/LOW或/BELOWNORMAL,这将提高您的系统响应能力。

#4


2  

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

除了前景/背景。另一种隐藏运行窗口的方法是通过vbscript,如果系统中仍然可用的话。

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

将其命名为sth.vbs,并从bat中调用它,并将其放入已完成的任务等。PersonallyI将在我管理的任何Windows系统中不加任何加速地禁用vbs:)

#5


0  

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

创建一个新的c# Windows应用程序,并从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();
}

#6


0  

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

这是我的Windows XP家庭安装,Unix的方式:

call notepad.exe & 

#7


0  

Actually is quite easy with this option at the end:

实际上,这个选项的结尾很简单:

c:\start BATCH.bat -WindowStyle Hidden

c:\开始批处理。蝙蝠-WindowStyle隐藏

#1


65  

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

实际上,下面的工作对我来说很好,并创建了新的窗口:

test.cmd:

test.cmd:

@echo off
call "cmd /c start test2.cmd"
call "cmd /c start test3.cmd"
echo Foo
pause

test2.cmd

test2.cmd

@echo off
echo Test 2
pause
exit

test3.cmd

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.

将其与参数结合起来,例如/min,如Moshe指出的,如果您不想让新窗口在您的前面生成。

#2


122  

Two years old, but for completeness...

两岁,但为了完整…

Standard, inline approach: (i.e. behaviour you'd get when using & in Linux)

标准的内联方法(即在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.).

注:1。调用与.bat文件配对,因为它通常在那里。(例如,这只是CMD /C调用的扩展名)。bat”表单让它异步。通常,需要正确地获取退出代码,但这不是问题所在。2。只有在名称包含空格时,才需要双引号。(这个名字可能是一条路径,在这种情况下,可能性更大。)

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)

如果您希望bat在独立的控制台运行:(即另一个窗口)

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:

注意:这实际上是一个糟糕的表单,除非您的用户特别想使用打开的窗口作为一个正常的控制台。如果你只是想让窗口停留在屏幕上以查看输出,最好在bat文件的末尾暂停一下。甚至,之后添加^ &暂停命令行:

START CMD /C CALL "foo.bat" [args [...]] ^& PAUSE

#3


5  

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.

因为START是在CMD脚本的背景下执行某些事情的唯一方法,所以我建议您继续使用它。不要使用/B修改器,尝试/MIN,这样新创建的窗口不会打扰到你。此外,您可以将优先级设置为/LOW或/BELOWNORMAL,这将提高您的系统响应能力。

#4


2  

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

除了前景/背景。另一种隐藏运行窗口的方法是通过vbscript,如果系统中仍然可用的话。

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

将其命名为sth.vbs,并从bat中调用它,并将其放入已完成的任务等。PersonallyI将在我管理的任何Windows系统中不加任何加速地禁用vbs:)

#5


0  

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

创建一个新的c# Windows应用程序,并从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();
}

#6


0  

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

这是我的Windows XP家庭安装,Unix的方式:

call notepad.exe & 

#7


0  

Actually is quite easy with this option at the end:

实际上,这个选项的结尾很简单:

c:\start BATCH.bat -WindowStyle Hidden

c:\开始批处理。蝙蝠-WindowStyle隐藏