Windows XP 或更高版本 Windows:如何在不显示窗口的情况下在后台运行批处理文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/298562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows XP or later Windows: How can I run a batch file in the background with no window displayed?
提问by VonC
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 argumentspassed 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. - From Windows, without triggering a second window, my solution is still valid.
- 从控制台窗口中的第二个脚本就
start /b
足够了。 - 在 Windows 中,没有触发第二个 window,我的解决方案仍然有效。
采纳答案by P Daddy
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 canuse start /b
second.batto 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推出第二批文件,一个从你的第一个共享您的第一个窗口同步。如果两个批处理文件同时写入控制台,输出将重叠并且可能无法辨认。此外,您需要exit
在第二个批处理文件的末尾放置一个命令,否则cmd
一旦一切完成,您就会在第二个shell 中。
回答by VonC
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 (
%*
)
- 脚本的 [path]\name
- 脚本所需的所有其他参数 (
%*
)
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"
- bWaitOnReturn : false means your first script does not need to wait for your second script to finish
- intWindowStyle : 0 表示“不可见的窗口”
- 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
回答by Rob Kam
Convert the batch file to an exe. Try Bat To Exe Converteror 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,并选择将其作为幽灵应用程序运行的选项,即无窗口。
回答by indago
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 文件名和路径。
回答by npocmaka
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 IEXPRESSsolution 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解决方案甚至可以在旧的 win 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/batch 混合与一些 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
回答by JosephStyons
Run it under a different user name, using "runas" or by scheduling it under a different user in Windows Scheduled Tasks.
在不同的用户名下运行它,使用“runas”或通过在 Windows 计划任务中的不同用户下安排它。
回答by wimh
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 useto connect to a remote server.
请注意,以这种方式启动的批处理文件以系统用户身份运行,这意味着您无法自动访问网络共享。但是您可以使用net use连接到远程服务器。
You have to download the Windows 2003 Resource Kitto get it. The Resource Kit can also be installed on other versions of windows, like Windows XP.
您必须下载 Windows 2003 Resource Kit才能获得它。Resource Kit 也可以安装在其他版本的 Windows 上,例如 Windows XP。
回答by Vinod Chelladurai
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
回答by user2928048
You also can use
你也可以使用
start /MIN notepad.exe
PS: Unfortunatly, minimized window status depends on command to run. V.G. doen't work
PS:不幸的是,最小化窗口状态取决于要运行的命令。VG 不工作
start /MIN calc.exe