windows 隐藏执行另一个 .EXE 文件的 .BAT 文件的命令窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/507347/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:01:06  来源:igfitidea点击:

Hide Command Window of .BAT file that Executes Another .EXE File

windowscommand-linecopyexecutable

提问by JeffO

This is a batch file in Windows.

这是 Windows 中的批处理文件。

Here is my .bat file

这是我的 .bat 文件

@echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"

"C:\ThirdParty.exe"

This works fine except the .bat file leaves the command window open the whole time the "ThirdParty" application is running.
I need the command window to close.

这工作正常,除了 .bat 文件在“第三方”应用程序运行的整个过程中都使命令窗口保持打开状态。
我需要关闭命令窗口。

I would use the short-cut for the application but I must be able to run this copy command first (it actually changes which data base and server to use for the application).

我将使用应用程序的快捷方式,但我必须能够首先运行此复制命令(它实际上更改了用于应用程序的数据库和服务器)。

The ThirdParty application does not allow the user to change the source of the db or the application server.

第三方应用程序不允许用户更改数据库或应用程序服务器的来源。

We're doing this to allow users to change from a test environment to the production environment.

我们这样做是为了允许用户从测试环境更改为生产环境。

采纳答案by cg.

Using startworks for me:

使用start对我有用:

@echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe

EDIT: Ok, looking more closely, startseems to interpret the first parameter as the new window title if quoted. So, if you need to quote the path to your ThirdParty.exe you must supply a title string as well.

编辑:好的,仔细观察,start如果引用的话,似乎将第一个参数解释为新窗口标题。因此,如果您需要引用 ThirdParty.exe 的路径,则还必须提供标题字符串。

Examples:

例子:

:: Title not needed:
start C:\ThirdParty.exe

:: Title needed
start "Third Party App" "C:\Program Files\Vendor\ThirdParty.exe"

回答by jlenfers

Create a .vbsfile with this code:

.vbs使用以下代码创建一个文件:

CreateObject("Wscript.Shell").Run "your_batch.bat",0,True

This .vbswill run your_batch.bathidden.

.vbsyour_batch.bat隐藏运行。

Works fine for me.

对我来说很好用。

回答by QAZ

Try this:

尝试这个:

@echo off 
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe
exit

回答by Josy

Great tip. It works with batch files that are running a java program also.

很棒的提示。它也适用于运行 java 程序的批处理文件。

start javaw -classpath "%CP%" main.Main

回答by mzuther

Using startworks fine, unless you are using a scripting language. Fortunately, there's a way out for Python - just use pythonw.exeinstead of python.exe:

start除非您使用脚本语言,否则使用效果很好。幸运的是,Python 有一个出路 - 只需使用pythonw.exe而不是python.exe

:: Title not needed:
start pythonw.exe application.py

In case you need quotes, do this:

如果您需要报价,请执行以下操作:

:: Title needed
start "Great Python App" pythonw.exe "C:\Program Files\Vendor\App\application.py"

回答by jamesdlin

You might be interested in trying my silentbatchprogram, which will run a .bat/.cmdscript, suppress creation of the Command Prompt window entirely (so you won't see it appear and then disappear), and optionally log the output to a specified file.

您可能有兴趣尝试我的Silentbatch程序,该程序将运行.bat/.cmd脚本,完全禁止创建命令提示符窗口(因此您不会看到它出现然后消失),并可选择将输出记录到指定文件。

回答by Paul Barnett

You can create a VBS script that will force the window to be hidden.

您可以创建一个 VBS 脚本来强制隐藏窗口。

Set WshShell = WScript.CreateObject("WScript.Shell")
obj = WshShell.Run("""C:\Program Files (x86)\McKesson\HRS
Distributed\SwE.bat""", 0)
set WshShell = Nothing

Then, rather than executing the batch file, execute the script.

然后,不是执行批处理文件,而是执行脚本。

回答by Egoadsr

Or you can use:

或者你可以使用:

Start /d "the directory of the executable" /b "the name of the executable" "parameters of the executable" %1

If %1 is a file then it is passed to your executable. For example in notepad.exe foo.txt%1 is "foo.txt".

如果 %1 是一个文件,那么它会被传递给您的可执行文件。例如在notepad.exe foo.txt%1 中是“foo.txt”。

The /bparameter of the start command does this:

/bstart 命令的参数执行以下操作:

Starts an application without opening a new Command Prompt window. CTRL+Chandling is ignored unless the application enables CTRL+Cprocessing. Use CTRL+BREAKto interrupt the application.

在不打开新的命令提示符窗口的情况下启动应用程序。CTRL+C处理将被忽略,除非应用程序启用CTRL+C处理。使用CTRL+BREAK中断应用程序。

Which is exactly what we want.

这正是我们想要的。

回答by Oskar Duveborn

I haven't really found a good way to do that natively, so I just use a utility called hstartwhich does it for me. If there's a neater way to do it, that would be nice.

我还没有真正找到一个很好的方法来在本地做到这一点,所以我只是使用一个名为hstart的实用程序来为我做这件事。如果有更简洁的方法来做到这一点,那就太好了。

回答by Joe

run it under a different user. assuming this is a windows box, create a user account for scheduled tasks. run it as that user. The command prompt will only show for the user currently logged in.

在不同的用户下运行它。假设这是一个 Windows 框,为计划任务创建一个用户帐户。以该用户身份运行它。命令提示符只会显示当前登录的用户。