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

发布于 2024-07-13 07:00:45 字数 370 浏览 7 评论 0原文

这是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

鯉魚旗 2024-07-20 07:00:45

使用 start 对我有用:

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

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

例子:

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

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

Using start works for me:

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

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:

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

:: Title needed
start "Third Party App" "C:\Program Files\Vendor\ThirdParty.exe"
两相知 2024-07-20 07:00:45

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

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

.vbs 将隐藏运行 your_batch.bat

对我来说效果很好。

Create a .vbs file with this code:

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

This .vbs will run your_batch.bat hidden.

Works fine for me.

凯凯我们等你回来 2024-07-20 07:00:45

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

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

如果您需要引号,请执行以下操作:

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

Using start works fine, unless you are using a scripting language. Fortunately, there's a way out for Python - just use pythonw.exe instead of 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"
护你周全 2024-07-20 07:00:45

尝试这个:

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

Try this:

@echo off 
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe
exit
寄居人 2024-07-20 07:00:45

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

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

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

start javaw -classpath "%CP%" main.Main
失去的东西太少 2024-07-20 07:00:45

您可能有兴趣尝试我的 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.

浸婚纱 2024-07-20 07:00:45

或者您可以使用:

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

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

启动命令的 /b 参数执行以下操作:

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

这正是我们想要的。

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

The /b parameter of the start command does this:

Starts an application without opening a new Command Prompt window. CTRL+C handling is ignored unless the application enables CTRL+C processing. Use CTRL+BREAK to interrupt the application.

Which is exactly what we want.

花想c 2024-07-20 07:00:45

我还没有真正找到一种本地执行此操作的好方法,因此我只使用名为 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.

如梦初醒的夏天 2024-07-20 07:00:45

使用 Windows API,我们可以启动新进程、控制台应用程序,并隐藏其“黑色”窗口。 这可以在进程创建时完成,并完全避免显示“黑色”窗口。

CreateProcess 函数中 dwCreationFlags 参数可以有 CREATE_NO_WINDOW 标志:

The process is a console application that is being run
without a console window. Therefore, the console handle
for the application is not set. This flag is ignored if
the application is not a console application

这是 hide-win32-console-window 可执行文件<的链接/a> 使用此方法和源代码

hide-win32-console-window 类似于 Jamesdlin 的silentbatch 程序

有一个悬而未决的问题:当程序的窗口不存在时,如何处理程序的输出?如果发生异常怎么办? 丢弃输出并不是一个好的解决方案。 hide-win32-console-window 使用匿名管道将程序的输出重定向到在当前目录中创建的文件。

用法

batchscript_starter.exe full/path/to/application [要传递的参数]

运行 python 脚本的示例

batchscript_starter.exe c:\Python27\python.exe -c "import time; print('prog start'); time.sleep(3.0); print('prog end');"

输出文件在工作目录中创建名为 python.2019-05-13-13-32-39.log ,包含 python 命令的输出:

prog start
prog end

运行命令示例

batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C dir .

输出文件在名为 cmd.2019-05 的工作目录中创建-13-13-37-28.log 以及 CMD 的输出:

 Volume in drive Z is Storage
 Volume Serial Number is XXXX-YYYY

 Directory of hide_console_project\hide-win32-console-window

2019-05-13  13:37    <DIR>          .
2019-05-13  13:37    <DIR>          ..
2019-05-13  04:41            17,274 batchscript_starter.cpp
2018-04-10  01:08            46,227 batchscript_starter.ico
2019-05-12  11:27             7,042 batchscript_starter.rc
2019-05-12  11:27             1,451 batchscript_starter.sln
2019-05-12  21:51             8,943 batchscript_starter.vcxproj
2019-05-12  21:51             1,664 batchscript_starter.vcxproj.filters
2019-05-13  03:38             1,736 batchscript_starter.vcxproj.user
2019-05-13  13:37                 0 cmd.2019-05-13-13-37-28.log
2019-05-13  04:34             1,518 LICENSE
2019-05-13  13:32                22 python.2019-05-13-13-32-39.log
2019-05-13  04:55                82 README.md
2019-05-13  04:44             1,562 Resource.h
2018-04-10  01:08            46,227 small.ico
2019-05-13  04:44               630 targetver.h
2019-05-13  04:57    <DIR>          x64
              14 File(s)        134,378 bytes
               3 Dir(s)  ???,???,692,992 bytes free

快捷方式示例

运行 .bat 脚本启动无窗口 .bat 文件的快捷方式

Target 字段:

C:\batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C C:\start_wiki.bat

指定的目录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:

The process is a console application that is being run
without a console window. Therefore, the console handle
for the application is not set. This flag is ignored if
the application is not a console application

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

batchscript_starter.exe c:\Python27\python.exe -c "import time; print('prog start'); time.sleep(3.0); print('prog end');"

The output file is created in working directory named python.2019-05-13-13-32-39.log with output from the python command:

prog start
prog end

Example running command

batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C dir .

The output file is created in working directory named cmd.2019-05-13-13-37-28.log with output from CMD:

 Volume in drive Z is Storage
 Volume Serial Number is XXXX-YYYY

 Directory of hide_console_project\hide-win32-console-window

2019-05-13  13:37    <DIR>          .
2019-05-13  13:37    <DIR>          ..
2019-05-13  04:41            17,274 batchscript_starter.cpp
2018-04-10  01:08            46,227 batchscript_starter.ico
2019-05-12  11:27             7,042 batchscript_starter.rc
2019-05-12  11:27             1,451 batchscript_starter.sln
2019-05-12  21:51             8,943 batchscript_starter.vcxproj
2019-05-12  21:51             1,664 batchscript_starter.vcxproj.filters
2019-05-13  03:38             1,736 batchscript_starter.vcxproj.user
2019-05-13  13:37                 0 cmd.2019-05-13-13-37-28.log
2019-05-13  04:34             1,518 LICENSE
2019-05-13  13:32                22 python.2019-05-13-13-32-39.log
2019-05-13  04:55                82 README.md
2019-05-13  04:44             1,562 Resource.h
2018-04-10  01:08            46,227 small.ico
2019-05-13  04:44               630 targetver.h
2019-05-13  04:57    <DIR>          x64
              14 File(s)        134,378 bytes
               3 Dir(s)  ???,???,692,992 bytes free

Example shortcut for running .bat script

Shortcut for starting windowless .bat file

Target field:

C:\batchscript_starter.exe C:\WINDOWS\system32\cmd.exe /C C:\start_wiki.bat

Directory specified in Start in field will hold output files.

长不大的小祸害 2024-07-20 07:00:45

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

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

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

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

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.

浅忆流年 2024-07-20 07:00:45

在不同的用户下运行它。 假设这是一个 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.

洛阳烟雨空心柳 2024-07-20 07:00:45

Compile the batch file to an executable using Batch2Exe http://www.f2ko.de/programs.php?lang=en&pid=b2e.
Use the "Invisible Window" option.

酷炫老祖宗 2024-07-20 07:00:45

要使执行 .exe 文件的 .bat 文件的命令窗口尽快退出,请在您尝试执行的文件之前使用 @start 行。 下面是一个示例:

(insert other code here)
@start executable.exe
(insert other code here)

您不必将其他代码与 @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:

(insert other code here)
@start executable.exe
(insert other code here)

You don't have to use other code with @start executable.exe.

深府石板幽径 2024-07-20 07:00:45

我用它从 C# 启动一个 cmd 文件:

Process proc = new Process();
proc.StartInfo.WorkingDirectory = "myWorkingDirectory";
proc.StartInfo.FileName = "myFileName.cmd";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();

I used this to start a cmd file from C#:

Process proc = new Process();
proc.StartInfo.WorkingDirectory = "myWorkingDirectory";
proc.StartInfo.FileName = "myFileName.cmd";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
友欢 2024-07-20 07:00:45

请使用这个,上面的不起作用。 我在Windows Server 2003上测试过。

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

Please use this one, the above does not work. I have tested in Window server 2003.

@echo off 
copy "C:\Remoting.config-Training" "C:\Remoting.config"
Start /I "" "C:\ThirdParty.exe"
exit
平定天下 2024-07-20 07:00:45

在启动命令中添加 /min 可以在我的 Windows 11 家庭版 上完全隐藏窗口

start /min C:\jdk1.8.0_221\bin\java -jar app.jar

Adding /min to the start command hides the window completely for me on my Windows 11 home edition

start /min C:\jdk1.8.0_221\bin\java -jar app.jar
蔚蓝源自深海 2024-07-20 07:00:45

将其放在 cmd 脚本的第一行:

%1 start "" mshta vbscript:createobject("wscript.shell").run("""%~s0"" ::",0)(window.close)&&exit

Put it on the first line of your cmd script:

%1 start "" mshta vbscript:createobject("wscript.shell").run("""%~s0"" ::",0)(window.close)&&exit
本王不退位尔等都是臣 2024-07-20 07:00:45

因此下面的 vbscript 将以隐藏模式启动 cmd/bat 文件。

strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)

'MsgBox(strFolder)

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & strFolder & "\a.bat" & Chr(34), 0
Set WshShell = Nothing

现在,只有应用程序窗口可见,而不是 cmd.exe 窗口

So below vbscript will launch the cmd/bat file in hidden mode.

strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)

'MsgBox(strFolder)

Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & strFolder & "\a.bat" & Chr(34), 0
Set WshShell = Nothing

Now, only app window will be visible, not the cmd.exe window

风启觞 2024-07-20 07:00:45

对于那些习惯肮脏解决方案的人来说,这是一个简单的解决方法。 按 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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文