Windows XP或更高版本Windows:如何在后台运行批处理文件而不显示窗口?

时间:2021-09-13 02:17:05

I know I have already answered a similar question (Running Batch File in background when windows boots up), but this time I need to launch a batch:

我知道我已经回答了类似的问题(当Windows启动时在后台运行批处理文件),但这次我需要启动批处理:

  • from another batch,
  • 从另一批,

  • without any console window displayed,
  • 没有显示任何控制台窗口,

  • with all arguments passed to the invisible batch.
  • 将所有参数传递给不可见批处理。

The first batch is executed in a console window. However, I do not want the second batch (launched by the first in a asynchronous way) to also display a console window.

第一批在控制台窗口中执行。但是,我不希望第二批(由第一批以异步方式启动)也显示控制台窗口。

I have come up with a VBScript script which does just that, and I put the script as an answer for others to refer to, but if you have other ideas/solutions, feel free to contribute.

我已经提出了一个VBScript脚本,它就是这样做的,我把脚本作为其他人参考的答案,但是如果你有其他的想法/解决方案,请随时贡献。

Note: The console window of Windows command processor is named not really correct DOS window by many people.

注意:Windows命令处理器的控制台窗口被很多人命名为不太正确的DOS窗口。


Thank you all for the answers. From what I understand, if I need to asynchronously call a script to run in an invisible mode:

谢谢大家的答案。根据我的理解,如果我需要异步调用脚本以在隐形模式下运行:

  • From a second script already in a console window, start /b is enough.
  • 从控制台窗口中的第二个脚本,start / b就足够了。

  • From Windows, without triggering a second window, my solution is still valid.
  • 在Windows中,没有触发第二个窗口,我的解决方案仍然有效。

8 个解决方案

#1


51  

Do you need the second batch file to run asynchronously? Typically one batch file runs another synchronously with the call command, and the second one would share the first one's window.

您是否需要第二批文件异步运行?通常,一个批处理文件与call命令同步运行另一个批处理文件,第二个批处理文件将共享第一个窗口。

You can use start /b second.bat to launch a second batch file asynchronously from your first that shares your first one's window. If both batch files write to the console simultaneously, the output will be overlapped and probably indecipherable. Also, you'll want to put an exit command at the end of your second batch file, or you'll be within a second cmd shell once everything is done.

您可以使用start / b second.bat从您的第一个批处理文件异步启动第二个批处理文件,该文件共享您的第一个窗口。如果两个批处理文件同时写入控制台,则输出将重叠并且可能难以辨认。此外,您需要在第二个批处理文件的末尾放置一个退出命令,或者在完成所有操作后,您将在第二个cmd shell中。

#2


108  

Here is a possible solution:

这是一个可能的解决方案:

From your first script, call your second script with the following line:

在第一个脚本中,使用以下行调用第二个脚本:

wscript.exe invis.vbs run.bat %*

Actually, you are calling a vbs script with:

实际上,您正在使用以下命令调用vbs脚本:

  • the [path]\name of your script
  • 脚本的[路径] \名称

  • all the other arguments needed by your script (%*)
  • 脚本所需的所有其他参数(%*)

Then, invis.vbs will call your script with the Windows Script Host Run() method, which takes:

然后,invis.vbs将使用Windows Script Host Run()方法调用您的脚本,该方法采用:

  • intWindowStyle : 0 means "invisible windows"
  • intWindowStyle:0表示“隐形窗口”

  • bWaitOnReturn : false means your first script does not need to wait for your second script to finish
  • bWaitOnReturn:false表示您的第一个脚本不需要等待第二个脚本完成

Here is invis.vbs:

这是invis.vbs:

set args = WScript.Arguments
num = args.Count

if num = 0 then
    WScript.Echo "Usage: [CScript | WScript] invis.vbs aScript.bat <some script arguments>"
    WScript.Quit 1
end if

sargs = ""
if num > 1 then
    sargs = " "
    for k = 1 to num - 1
        anArg = args.Item(k)
        sargs = sargs & anArg & " "
    next
end if

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False

#3


12  

Convert the batch file to an exe. Try Bat To Exe Converter or Online Bat To Exe Converter, and choose the option to run it as a ghost application, i.e. no window.

将批处理文件转换为exe。尝试Bat To Exe Converter或Online Bat To Exe Converter,并选择将其作为ghost应用程序运行,即无窗口。

#4


8  

I think this is the easiest and shortest solution to running a batch file without opening the DOS window, it can be very distracting when you want to schedule a set of commands to run periodically, so the DOS window keeps poping up, here is your solution. Use a VBS Script to call the batch file ...

我认为这是在不打开DOS窗口的情况下运行批处理文件的最简单,最简单的解决方案,当您想要安排一组定期运行的命令时,它会非常分散注意力,因此DOS窗口会不断弹出,这是您的解决方案。使用VBS脚本调用批处理文件...

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Run chr(34) & "C:\Batch Files\ mycommands.bat" & Chr(34), 0 
Set WshShell = Nothing 

Copy the lines above to an editor and save the file with .VBS extension. Edit the .BAT file name and path accordingly.

将上面的行复制到编辑器并使用.VBS扩展名保存文件。相应地编辑.BAT文件名和路径。

#5


7  

Here's my collection of ways to achieve that - and even more - where it was possible I've tried to return also the PID of the started process (all linked scripts can be downloaded and saved with whatever name you find convenient):

这是我实现这一目标的方法集合 - 甚至更多 - 我可能已经尝试返回已启动进程的PID(所有链接的脚本都可以下载并以您认为方便的名称保存):

1) The IEXPRESS solution can be used even on old win 95/98 machines. Iexpress is a really ancient tool that is still packaged with Windows - as arguments accepts only the command and its arguments.

1)IEXPRESS解决方案甚至可以在旧的95/98机器上使用。 Iexpress是一个非常古老的工具,仍然与Windows一起打包 - 因为参数只接受命令及其参数。

Example usage:

call IEXPhidden.bat "cmd /c myBat.bat"  "argument"

2) SCHTASKS - Again accepts only two arguments - the command and the arguments.Also checks if it's started with elevated permissions and if possible gets the PID of the process with WEVTUTIL (available from Vista and above so the newer version of windows will receive the PID) command.

2)SCHTASKS - 再次只接受两个参数 - 命令和参数。还检查它是否以提升的权限启动,如果可能的话,使用WEVTUTIL获取进程的PID(可从Vista及以上版本获得,因此较新版本的Windows将接收PID)命令。

Example usage:

call SCHPhidden.bat "cmd /c myBat.bat"  "argument"

3) 'WScript.Shell' - the script is full wrapper of 'WScript.Shell' and every possible option can be set through the command line options.It's a jscript/batch hybrid and can be called as a bat.

3)'WScript.Shell' - 脚本是'WScript.Shell'的完整包装,每个可能的选项都可以通过命令行选项设置。它是一个jscript / batch混合,可以称为bat。

Example usage (for more info print the help with '-h'):

示例用法(有关详细信息,请使用'-h'打印帮助):

call ShellRunJS.bat "notepad.exe" -style 0 -wait no 

4) 'Win32_ProcessStartup' - again full wrapper and all options are accessible through the command line arguments.This time it's WSF/batch hybrid with some Jscript and some VBScript pieces of code - but it returns the PID of the started process.If process is not hidden some options like X/Y coordinates can be used (not applicable for every executable - but for example cmd.exe accepts coordinates).

4)'Win32_ProcessStartup' - 再次完全包装,所有选项都可以通过命令行参数访问。这次是WSF /批处理混合,带有一些Jscript和一些VBScript代码 - 但它返回已启动进程的PID。如果进程是没有隐藏一些选项,如X / Y坐标可以使用(不适用于每个可执行文件 - 但例如cmd.exe接受坐标)。

Example usage (for more info print the help with '-h'):

示例用法(有关详细信息,请使用'-h'打印帮助):

call win32process.bat "notepad" -arguments "/A openFile.txt"  -showWindows 0 -title "notepad"

5) The .NET solution . Most of the options of ProcessStartInfo options are used (but at the end I was too tired to include everything):

5).NET解决方案。使用了ProcessStartInfo选项的大多数选项(但最后我太累了,无法包含所有内容):

Example usage (for more info print the help with '-h'):

示例用法(有关详细信息,请使用'-h'打印帮助):

call ProcessStartJS.bat "notepad" -arguments "/A openFile.txt"  -style Hidden -directory "." -title "notepad" -priority Normal

#6


2  

In the other question I suggested autoexnt. That is also possible in this situation. Just set the service to run manually (ie not automatic at startup). When you want to run your batch, modify the autoexnt.bat file to call the batch file you want, and start the autoexnt service.

在另一个问题我建议autoexnt。在这种情况下也是可能的。只需将服务设置为手动运行(即启动时不自动)。如果要运行批处理,请修改autoexnt.bat文件以调用所需的批处理文件,然后启动autoexnt服务。

The batchfile to start this, can look like this (untested):

启动它的批处理文件可能看起来像这样(未经测试):

echo call c:\path\to\batch.cmd %* > c:\windows\system32\autoexnt.bat
net start autoexnt

Note that batch files started this way run as the system user, which means you do not have access to network shares automatically. But you can use net use to connect to a remote server.

请注意,以这种方式启动的批处理文件以系统用户身份运行,这意味着您无法自动访问网络共享。但您可以使用net use连接到远程服务器。

You have to download the Windows 2003 Resource Kit to get it. The Resource Kit can also be installed on other versions of windows, like Windows XP.

您必须下载Windows 2003资源工具包才能获得它。 Resource Kit也可以安装在其他版本的Windows上,例如Windows XP。

#7


2  

Run it under a different user name, using "runas" or by scheduling it under a different user in Windows Scheduled Tasks.

使用“runas”或在Windows Scheduled Tasks中的其他用户下安排它,在不同的用户名下运行它。

#8


0  

You can run your .bat file through a .vbs file
Copy the following code into your .vbs file :

您可以通过.vbs文件运行.bat文件将以下代码复制到.vbs文件中:

Dim WshShell
Dim obj
Set WshShell = WScript.CreateObject("WScript.Shell") 
obj = WshShell.Run("C:\Users\file1.bat", 0) 
obj = WshShell.Run("C:\Users\file2.bat", 0)  and so on
set WshShell = Nothing 

#1


51  

Do you need the second batch file to run asynchronously? Typically one batch file runs another synchronously with the call command, and the second one would share the first one's window.

您是否需要第二批文件异步运行?通常,一个批处理文件与call命令同步运行另一个批处理文件,第二个批处理文件将共享第一个窗口。

You can use start /b second.bat to launch a second batch file asynchronously from your first that shares your first one's window. If both batch files write to the console simultaneously, the output will be overlapped and probably indecipherable. Also, you'll want to put an exit command at the end of your second batch file, or you'll be within a second cmd shell once everything is done.

您可以使用start / b second.bat从您的第一个批处理文件异步启动第二个批处理文件,该文件共享您的第一个窗口。如果两个批处理文件同时写入控制台,则输出将重叠并且可能难以辨认。此外,您需要在第二个批处理文件的末尾放置一个退出命令,或者在完成所有操作后,您将在第二个cmd shell中。

#2


108  

Here is a possible solution:

这是一个可能的解决方案:

From your first script, call your second script with the following line:

在第一个脚本中,使用以下行调用第二个脚本:

wscript.exe invis.vbs run.bat %*

Actually, you are calling a vbs script with:

实际上,您正在使用以下命令调用vbs脚本:

  • the [path]\name of your script
  • 脚本的[路径] \名称

  • all the other arguments needed by your script (%*)
  • 脚本所需的所有其他参数(%*)

Then, invis.vbs will call your script with the Windows Script Host Run() method, which takes:

然后,invis.vbs将使用Windows Script Host Run()方法调用您的脚本,该方法采用:

  • intWindowStyle : 0 means "invisible windows"
  • intWindowStyle:0表示“隐形窗口”

  • bWaitOnReturn : false means your first script does not need to wait for your second script to finish
  • bWaitOnReturn:false表示您的第一个脚本不需要等待第二个脚本完成

Here is invis.vbs:

这是invis.vbs:

set args = WScript.Arguments
num = args.Count

if num = 0 then
    WScript.Echo "Usage: [CScript | WScript] invis.vbs aScript.bat <some script arguments>"
    WScript.Quit 1
end if

sargs = ""
if num > 1 then
    sargs = " "
    for k = 1 to num - 1
        anArg = args.Item(k)
        sargs = sargs & anArg & " "
    next
end if

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False

#3


12  

Convert the batch file to an exe. Try Bat To Exe Converter or Online Bat To Exe Converter, and choose the option to run it as a ghost application, i.e. no window.

将批处理文件转换为exe。尝试Bat To Exe Converter或Online Bat To Exe Converter,并选择将其作为ghost应用程序运行,即无窗口。

#4


8  

I think this is the easiest and shortest solution to running a batch file without opening the DOS window, it can be very distracting when you want to schedule a set of commands to run periodically, so the DOS window keeps poping up, here is your solution. Use a VBS Script to call the batch file ...

我认为这是在不打开DOS窗口的情况下运行批处理文件的最简单,最简单的解决方案,当您想要安排一组定期运行的命令时,它会非常分散注意力,因此DOS窗口会不断弹出,这是您的解决方案。使用VBS脚本调用批处理文件...

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Run chr(34) & "C:\Batch Files\ mycommands.bat" & Chr(34), 0 
Set WshShell = Nothing 

Copy the lines above to an editor and save the file with .VBS extension. Edit the .BAT file name and path accordingly.

将上面的行复制到编辑器并使用.VBS扩展名保存文件。相应地编辑.BAT文件名和路径。

#5


7  

Here's my collection of ways to achieve that - and even more - where it was possible I've tried to return also the PID of the started process (all linked scripts can be downloaded and saved with whatever name you find convenient):

这是我实现这一目标的方法集合 - 甚至更多 - 我可能已经尝试返回已启动进程的PID(所有链接的脚本都可以下载并以您认为方便的名称保存):

1) The IEXPRESS solution can be used even on old win 95/98 machines. Iexpress is a really ancient tool that is still packaged with Windows - as arguments accepts only the command and its arguments.

1)IEXPRESS解决方案甚至可以在旧的95/98机器上使用。 Iexpress是一个非常古老的工具,仍然与Windows一起打包 - 因为参数只接受命令及其参数。

Example usage:

call IEXPhidden.bat "cmd /c myBat.bat"  "argument"

2) SCHTASKS - Again accepts only two arguments - the command and the arguments.Also checks if it's started with elevated permissions and if possible gets the PID of the process with WEVTUTIL (available from Vista and above so the newer version of windows will receive the PID) command.

2)SCHTASKS - 再次只接受两个参数 - 命令和参数。还检查它是否以提升的权限启动,如果可能的话,使用WEVTUTIL获取进程的PID(可从Vista及以上版本获得,因此较新版本的Windows将接收PID)命令。

Example usage:

call SCHPhidden.bat "cmd /c myBat.bat"  "argument"

3) 'WScript.Shell' - the script is full wrapper of 'WScript.Shell' and every possible option can be set through the command line options.It's a jscript/batch hybrid and can be called as a bat.

3)'WScript.Shell' - 脚本是'WScript.Shell'的完整包装,每个可能的选项都可以通过命令行选项设置。它是一个jscript / batch混合,可以称为bat。

Example usage (for more info print the help with '-h'):

示例用法(有关详细信息,请使用'-h'打印帮助):

call ShellRunJS.bat "notepad.exe" -style 0 -wait no 

4) 'Win32_ProcessStartup' - again full wrapper and all options are accessible through the command line arguments.This time it's WSF/batch hybrid with some Jscript and some VBScript pieces of code - but it returns the PID of the started process.If process is not hidden some options like X/Y coordinates can be used (not applicable for every executable - but for example cmd.exe accepts coordinates).

4)'Win32_ProcessStartup' - 再次完全包装,所有选项都可以通过命令行参数访问。这次是WSF /批处理混合,带有一些Jscript和一些VBScript代码 - 但它返回已启动进程的PID。如果进程是没有隐藏一些选项,如X / Y坐标可以使用(不适用于每个可执行文件 - 但例如cmd.exe接受坐标)。

Example usage (for more info print the help with '-h'):

示例用法(有关详细信息,请使用'-h'打印帮助):

call win32process.bat "notepad" -arguments "/A openFile.txt"  -showWindows 0 -title "notepad"

5) The .NET solution . Most of the options of ProcessStartInfo options are used (but at the end I was too tired to include everything):

5).NET解决方案。使用了ProcessStartInfo选项的大多数选项(但最后我太累了,无法包含所有内容):

Example usage (for more info print the help with '-h'):

示例用法(有关详细信息,请使用'-h'打印帮助):

call ProcessStartJS.bat "notepad" -arguments "/A openFile.txt"  -style Hidden -directory "." -title "notepad" -priority Normal

#6


2  

In the other question I suggested autoexnt. That is also possible in this situation. Just set the service to run manually (ie not automatic at startup). When you want to run your batch, modify the autoexnt.bat file to call the batch file you want, and start the autoexnt service.

在另一个问题我建议autoexnt。在这种情况下也是可能的。只需将服务设置为手动运行(即启动时不自动)。如果要运行批处理,请修改autoexnt.bat文件以调用所需的批处理文件,然后启动autoexnt服务。

The batchfile to start this, can look like this (untested):

启动它的批处理文件可能看起来像这样(未经测试):

echo call c:\path\to\batch.cmd %* > c:\windows\system32\autoexnt.bat
net start autoexnt

Note that batch files started this way run as the system user, which means you do not have access to network shares automatically. But you can use net use to connect to a remote server.

请注意,以这种方式启动的批处理文件以系统用户身份运行,这意味着您无法自动访问网络共享。但您可以使用net use连接到远程服务器。

You have to download the Windows 2003 Resource Kit to get it. The Resource Kit can also be installed on other versions of windows, like Windows XP.

您必须下载Windows 2003资源工具包才能获得它。 Resource Kit也可以安装在其他版本的Windows上,例如Windows XP。

#7


2  

Run it under a different user name, using "runas" or by scheduling it under a different user in Windows Scheduled Tasks.

使用“runas”或在Windows Scheduled Tasks中的其他用户下安排它,在不同的用户名下运行它。

#8


0  

You can run your .bat file through a .vbs file
Copy the following code into your .vbs file :

您可以通过.vbs文件运行.bat文件将以下代码复制到.vbs文件中:

Dim WshShell
Dim obj
Set WshShell = WScript.CreateObject("WScript.Shell") 
obj = WshShell.Run("C:\Users\file1.bat", 0) 
obj = WshShell.Run("C:\Users\file2.bat", 0)  and so on
set WshShell = Nothing