隐藏执行另一个 .EXE 文件的 .BAT 文件的命令窗口
这是 Windows 中的批处理文件。
这是我的 .bat 文件
@echo off
copy "C:\Remoting.config-Training" "C:\Remoting.config"
"C:\ThirdParty.exe"
这工作正常,只是 .bat 文件在“第三方”应用程序运行期间使命令窗口保持打开状态。
我需要关闭命令窗口。
我将使用应用程序的快捷方式,但我必须能够首先运行此复制命令(它实际上更改了应用程序使用的数据库和服务器)。
第三方应用程序不允许用户更改数据库或应用程序服务器的源。
我们这样做是为了允许用户从测试环境更改为生产环境。
This is a batch file in Windows.
Here is my .bat file
@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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
使用
start
对我有用:编辑:好的,仔细观察,
start
似乎将第一个参数解释为新的窗口标题(如果引用)。 因此,如果您需要引用 ThirdParty.exe 的路径,您还必须提供标题字符串。例子:
Using
start
works for me:EDIT: Ok, looking more closely,
start
seems 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.Examples:
使用以下代码创建一个
.vbs
文件:此
.vbs
将隐藏运行your_batch.bat
。对我来说效果很好。
Create a
.vbs
file with this code:This
.vbs
will runyour_batch.bat
hidden.Works fine for me.
除非您使用脚本语言,否则使用
start
效果很好。 幸运的是,Python 有一个出路 - 只需使用pythonw.exe
而不是python.exe
:如果您需要引号,请执行以下操作:
Using
start
works fine, unless you are using a scripting language. Fortunately, there's a way out for Python - just usepythonw.exe
instead ofpython.exe
:In case you need quotes, do this:
尝试这个:
Try this:
很棒的提示。 它也适用于运行 java 程序的批处理文件。
Great tip. It works with batch files that are running a java program also.
您可能有兴趣尝试我的 silentbatch 程序,该程序将运行
.bat< /code>/
.cmd
脚本,完全禁止创建命令提示符窗口(这样您就不会看到它出现然后消失),并可以选择将输出记录到指定文件。You might be interested in trying my silentbatch program, which will run a
.bat
/.cmd
script, 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.或者您可以使用:
如果 %1 是一个文件,则它将传递给您的可执行文件。 例如,在
notepad.exe foo.txt
中,%1 是“foo.txt”。启动命令的
/b
参数执行以下操作:这正是我们想要的。
Or you can use:
If %1 is a file then it is passed to your executable. For example in
notepad.exe foo.txt
%1 is "foo.txt".The
/b
parameter of the start command does this:Which is exactly what we want.
我还没有真正找到一种本地执行此操作的好方法,因此我只使用名为 hstart 为我做这件事。 如果有一种更简洁的方法来做到这一点,那就太好了。
I haven't really found a good way to do that natively, so I just use a utility called hstart which does it for me. If there's a neater way to do it, that would be nice.
使用 Windows API,我们可以启动新进程、控制台应用程序,并隐藏其“黑色”窗口。 这可以在进程创建时完成,并完全避免显示“黑色”窗口。
在 CreateProcess 函数中
dwCreationFlags
参数可以有 CREATE_NO_WINDOW 标志:这是 hide-win32-console-window 可执行文件<的链接/a> 使用此方法和源代码。
hide-win32-console-window
类似于 Jamesdlin 的silentbatch 程序。有一个悬而未决的问题:当程序的窗口不存在时,如何处理程序的输出?如果发生异常怎么办? 丢弃输出并不是一个好的解决方案。
hide-win32-console-window
使用匿名管道将程序的输出重定向到在当前目录中创建的文件。用法
batchscript_starter.exe full/path/to/application [要传递的参数]
运行 python 脚本的示例
输出文件在工作目录中创建名为
python.2019-05-13-13-32-39.log
,包含 python 命令的输出:运行命令示例
输出文件在名为
cmd.2019-05 的工作目录中创建-13-13-37-28.log
以及 CMD 的输出:快捷方式示例
运行 .bat 脚本
Target
字段:指定的目录
Start in
字段将保存输出文件。Using Windows API we can start new process, a console application, and hide its "black" window. This can be done at process creation and avoid showing "black" window at all.
In CreateProcess function the
dwCreationFlags
parameter can have CREATE_NO_WINDOW flag:Here is a link to hide-win32-console-window executable using this method and source code.
hide-win32-console-window
is similar to Jamesdlin's silentbatch program.There is open question: what to do with program's output when its window does not exist? What if exceptions happen? Not a good solution to throw away the output.
hide-win32-console-window
uses anonymous pipes to redirect program's output to file created in current directory.Usage
batchscript_starter.exe full/path/to/application [arguments to pass on]
Example running python script
The output file is created in working directory named
python.2019-05-13-13-32-39.log
with output from the python command:Example running command
The output file is created in working directory named
cmd.2019-05-13-13-37-28.log
with output from CMD:Example shortcut for running .bat script
Target
field:Directory specified in
Start in
field will hold output files.您可以创建一个 VBS 脚本来强制隐藏窗口。
然后,不执行批处理文件,而是执行脚本。
You can create a VBS script that will force the window to be hidden.
Then, rather than executing the batch file, execute the script.
在不同的用户下运行它。 假设这是一个 Windows 盒子,为计划任务创建一个用户帐户。 以该用户身份运行它。 命令提示符只会针对当前登录的用户显示。
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.
使用 Batch2Exe http://www.f2ko 将批处理文件编译为可执行文件。 de/programs.php?lang=en&pid=b2e。
使用“隐形窗口”选项。
Compile the batch file to an executable using Batch2Exe http://www.f2ko.de/programs.php?lang=en&pid=b2e.
Use the "Invisible Window" option.
要使执行 .exe 文件的 .bat 文件的命令窗口尽快退出,请在您尝试执行的文件之前使用
@start
行。 下面是一个示例:您不必将其他代码与
@startexecutable.exe
一起使用。To make the command window of a .bat file that executes a .exe file exit out as fast as possible, use the line
@start
before the file you're trying to execute. Here is an example:You don't have to use other code with
@start executable.exe
.我用它从 C# 启动一个 cmd 文件:
I used this to start a cmd file from C#:
请使用这个,上面的不起作用。 我在Windows Server 2003上测试过。
Please use this one, the above does not work. I have tested in Window server 2003.
在启动命令中添加 /min 可以在我的 Windows 11 家庭版 上完全隐藏窗口
Adding /min to the start command hides the window completely for me on my Windows 11 home edition
将其放在 cmd 脚本的第一行:
Put it on the first line of your cmd script:
因此下面的 vbscript 将以隐藏模式启动 cmd/bat 文件。
现在,只有应用程序窗口可见,而不是 cmd.exe 窗口
So below vbscript will launch the cmd/bat file in hidden mode.
Now, only app window will be visible, not the cmd.exe window
对于那些习惯肮脏解决方案的人来说,这是一个简单的解决方法。 按 win+tab,将 bat 文件拖放到新桌面上,然后就不用管它了。
我偶尔会制作一个很少使用的bat文件,并且必须使用工具来隐藏窗口有点麻烦。 这对我来说并不比需要的更复杂。
This is an easy work around for those who are fine with a dirty solution. Press win+tab, drag and drop the bat file to a new desktop and forget about it.
I occasionally will make a bat file that I'll rarely use, and it is kind of a drag to have to use tools to make the window hidden. This is no more complicated than it needs to be for me.