windows 如何避免在 cmd.exe 上弹出命令窗口

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

How to avoid command window popping on cmd.exe

windowscommand-linebatch-filewindows-installer

提问by sambha

I have a command as:

我有一个命令:

cmd.exe /c ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here

But when executed it opens a command window. Is there a way to avoid the command window from popping up?

但是在执行时它会打开一个命令窗口。有没有办法避免弹出命令窗口?

PS: I cannot remove cmd.exe from there. As you can see, I am trying to tie two commands one after the other in the same string.

PS:我无法从那里删除 cmd.exe。如您所见,我试图将两个命令一个接一个地连接到同一个字符串中。

Thanks.

谢谢。

Edit:Sorry. Its not a bat file. I want to execute 2 commands in the "UninstallString" of msiexec. I was trying so many things that my question got a bit carried away.

编辑:对不起。它不是一个bat文件。我想在 msiexec 的“UninstallString”中执行 2 个命令。我尝试了很多事情,以至于我的问题有点不知所措。

The command is:

命令是:

msiexec <product> <package> && reg delete /xxx

回答by mojo

The simplest option is to start the thing minimized. Shortcut Target:

最简单的选择是开始最小化的东西。快捷方式目标:

cmd /c START /MIN \path\to\test.bat

or

或者

cmd /c START /MIN cmd /k ( ping 1.1.1.1 -w 10000 -n 1 && @ECHO All OK)

It's not hidden or anything, but it doesn't show up on the desktop or—worse—steal the focus.

它没有隐藏或任何东西,但它没有显示在桌面上,或者——更糟糕的是——窃取焦点。

If you want the window to go away on its own, "cmd /c ..." will make that happen. "cmd /k ..." will leave the window open.

如果您希望窗口自行消失,“cmd /c ...” 将实现这一点。“cmd /k ...”将使窗口保持打开状态。

Plan B is referred to by @CodyGray in the SU link he posted. There are programs that don't open windows, like wperl, pythonw, or wscript (natively available on Windows). If you can pass your command through to one of those things, then you could effectively double-click an icon and have it run "silently."

@CodyGray 在他发布的 SU 链接中提到了 B 计划。有些程序不能打开窗口,例如 wperl、pythonw 或 wscript(在 Windows 上本地可用)。如果您可以将您的命令传递给其中之一,那么您可以有效地双击一个图标并让它“静默”运行。

If Perl's available, I'd certainly go with that because you can craft some pretty powerful one-liners that won't require creating other files.

如果 Perl 可用,我当然会选择它,因为您可以制作一些非常强大的单行程序,而无需创建其他文件。

wperl -MWin32 -MNet::Ping -e "$p=Net::Ping->new('icmp',10000); if ($p->ping('192.168.1.1')) { Win32::MsgBox('Ping Successful', 1 + MB_OK, 'All Good'); }"

In your example, you're chaining commands together, the latter is a notification. If you don't want to have a window open for the first command, it would be awkward to do it for the second when you're notifying the user of something. Having the process call "cmd /c start cmd /c @echo Everything's OK" would probably do it, but using CMD windows for user notification is probably not something the HCI guys would smile at.

在您的示例中,您将命令链接在一起,后者是一个通知。如果您不想为第一个命令打开一个窗口,那么当您通知用户某事时,为第二个命令打开窗口会很尴尬。让进程调用“cmd /c start cmd /c @echo Everything's OK”可能会这样做,但使用 CMD 窗口进行用户通知可能不是 HCI 人员会微笑的事情。

回答by Andreas Rejbrand

No, all batch files open in command-line windows; this has nothing to do with the presence of cmd.exein your particular file. A batch file is simply a number of command-line commands, one per line.

不,所有批处理文件都在命令行窗口中打开;这与cmd.exe您的特定文件中的存在无关。批处理文件只是许多命令行命令,每行一个。

I don't understand why you write test.batthe way you do. I'd rather expect

我不明白你为什么这样写test.bat。我宁愿期待

ping 1.1.1.1 -n 1 -w 10000
echo second command goes here

If, for some bizzare reason, you really need to use only a single line, you can simply do

如果由于某种奇怪的原因,你真的只需要使用一行,你可以简单地做

ping 1.1.1.1 -n 1 -w 10000 && echo second command goes here

回答by jamesdlin

As Andreas Rejbrandalready explained, the command prompt window is not from the explicit cmd.exeinvocation within your script but from executing the .batscript itself. (And despite your claim, you haven't provided any evidence why explicitly invoking cmd.exeis necessary. The whole point of a .batscript is to batchcommands together.)

正如Andreas Rejbrand已经解释的那样,命令提示符窗口不是来自cmd.exe脚本中的显式调用,而是来自执行.bat脚本本身。(尽管您提出了要求,但您没有提供任何证据表明为什么需要显式调用cmd.exe.bat脚本的全部意义在于将命令批处理在一起。)

That said, the silentbatchprogram that Paul Miner and I wrote can execute batch scripts and suppress the command prompt window. To use it, you would have to create a Windows shortcut that invokes silentbatch.exe test.batand double-click on that rather than double-clicking on test.batdirectly, however.

也就是说,Paul Miner 和我编写的silentbatch程序可以执行批处理脚本并抑制命令提示符窗口。但是,要使用它,您必须创建一个 Windows 快捷方式来调用silentbatch.exe test.bat并双击它,而不是test.bat直接双击。

回答by San

You could also create a shortcut to you batch script and then in the properties select to start the app minimized. It is explained here: CNET: How to automatically start a program minimized in Windows

您还可以创建批处理脚本的快捷方式,然后在属性中选择以最小化启动应用程序。解释如下:CNET:如何自动启动在 Windows 中最小化的程序

Step 1: Right-click on the shortcut of the program you want to start minimized and select Properties.
Step 2: Click on the drop-down menu under Run.
Step 3: Select "Minimized," then click the OK button.

步骤1:右键单击要最小化的程序的快捷方式,然后选择“属性”。
第 2 步:单击“运行”下的下拉菜单。
第 3 步:选择“最小化”,然后单击“确定”按钮。

回答by Zimba

From win cmd:

从赢得 cmd:

start /b cmd.exe /c "ping 1.1.1.1 -n 1 -w 10000 & echo second command goes here"

runs both commands without opening another cmd window, leaving parent window unblocked for further commands without waiting.

在不打开另一个 cmd 窗口的情况下运行这两个命令,无需等待即可让父窗口畅通无阻地执行进一步的命令。