63

这是 Windows 中的批处理文件。

这是我的 .bat 文件

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

"C:\ThirdParty.exe"

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

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

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

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

4

17 回答 17

51

使用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"
于 2009-02-03T14:52:06.660 回答
30

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

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

.vbsyour_batch.bat隐藏。

对我来说很好。

于 2013-11-18T18:45:00.697 回答
11

使用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"
于 2009-08-24T10:32:18.137 回答
10

试试这个:

@echo off 
copy "C:\Remoting.config-Training" "C:\Remoting.config"
start C:\ThirdParty.exe
exit
于 2009-02-03T14:51:12.807 回答
8

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

start javaw -classpath "%CP%" main.Main
于 2011-01-06T06:51:01.500 回答
5

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

于 2013-04-07T05:06:09.990 回答
4

或者您可以使用:

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”。

/bstart 命令的参数是这样做的:

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

这正是我们想要的。

于 2017-11-15T11:14:34.280 回答
3

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

于 2009-02-03T14:51:04.977 回答
3

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

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

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

于 2013-10-14T15:17:08.810 回答
3

使用 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 可执行文件的链接。

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.logpython 命令的输出命名的工作目录中创建:

prog start
prog end

示例运行命令

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

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

 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将保存输出文件。

于 2019-05-13T11:52:20.883 回答
2

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

于 2010-11-29T20:39:41.957 回答
2

使用 Batch2Exe http://www.f2ko.de/programs.php?lang=en&pid=b2e将批处理文件编译为可执行文件。使用“不可见窗口”选项。

于 2011-07-30T11:20:53.250 回答
2

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

Process proc = new Process();
proc.StartInfo.WorkingDirectory = "myWorkingDirectory";
proc.StartInfo.FileName = "myFileName.cmd";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
于 2018-05-02T18:20:40.543 回答
1

请使用这个,上面的不起作用。我已经在 Window server 2003 中进行了测试。

@echo off 
copy "C:\Remoting.config-Training" "C:\Remoting.config"
Start /I "" "C:\ThirdParty.exe"
exit
于 2009-08-10T10:46:09.800 回答
1

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

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

您不必将其他代码与@start executable.exe.

于 2017-03-06T23:19:19.100 回答
0

所以下面的 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 窗口

于 2021-08-02T11:16:15.757 回答
0

对于那些对肮脏解决方案感到满意的人来说,这是一个简单的解决方法。按win+tab,将bat文件拖放到新桌面,然后忘记它。

我偶尔会制作一个我很少使用的bat文件,不得不使用工具来隐藏窗口是一种拖累。这并不比我需要的复杂。

在此处输入图像描述

于 2021-10-06T04:47:00.697 回答