命令提示符运行文件_我可以在没有可见命令提示符的情况下运行Windows批处理文件吗?...

命令提示符运行文件

命令提示符运行文件

Batch files are a handy way to execute a series of commands in Windows, but is there anyway to run them invisibly in the background? Read on find out how.

批处理文件是在Windows中执行一系列命令的便捷方法,但是是否有在后台隐式运行它们的方法? 继续阅读以了解如何。

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

问题 (The Question)

SuperUser reader Jake wants to run a BAT file invisibly, he writes:

超级用户读者Jake希望以不可见的方式运行BAT文件,他写道:

I have installed a ruby gem called Redcar, which is launched from the command line. When it runs, it steals the shell until it terminates, so I have to create a new shell window to continue doing command line work. The shell I’m using is the GITBash shell from MySysGit.

我安装了一个名为Redcar的Ruby,它是从命令行启动的。 当它运行时,它会窃取外壳,直到终止为止,因此我必须创建一个新的外壳窗口以继续进行命令行工作。 我使用的外壳是MySysGit的GITBash外壳。

I found a Redcar.bat file which is meant to launch Redcar as a shortcut, I presume, but I don’t want the extra command prompt window to open whenever I launch the BAT file.

我想我找到了一个Redcar.bat文件,该文件旨在作为快捷方式启动Redcar,但是我不希望每次启动BAT文件时都打开额外的命令提示符窗口。

How do I just run the BAT without seeing the prompt?

我如何不查看提示就运行BAT?

Is there a solution for Jake’s stealthy prompt desire?

杰克隐秘的即时愿望有解决方案吗?

答案 (The Answers)

SuperUser contributor Afrazier responds with a combination of bad and good news:

超级用户贡献者Afrazier的坏消息和好消息相结合:

You can’t — executing a batch file with the built in Command Prompt is going to keep a window open until the batch file exits.

您不能-使用内置的命令提示符执行批处理文件将使窗口保持打开状态,直到批处理文件退出。

What you can do is take steps to make sure that the batch file exits as quickly as possible. If at all possible, modify the batch file to run whatever program with the start command. By default, start returns immediately without waiting for the program to exit, so the batch file will continue to run and, presumably, exit immediately. Couple that with modifying your shortcut to run the batch file minimized, and you’ll only see the taskbar flash without even seeing a window onscreen.

可以采取的步骤来确保批处理文件尽快退出。 如果有可能,请使用start命令修改批处理文件以运行任何程序。 默认情况下, start立即返回而无需等待程序退出,因此批处理文件将继续运行,并且可能立即退出。 再加上修改快捷方式以最小化运行批处理文件,您将只会看到任务栏闪烁,甚至看不到屏幕上的窗口。

One caveat to this is that if you’re running a console-mode program, which many script interpreters are, the batch file will wait for the program to exit, and using start will spawn a new console window. What you need to do in this case is run the Windows-based version of the interpreter instead of the console-based one — no start necessary. For Perl, you would run wperl.exe instead of perl.exe. For Python, it’s pythonw.exe instead of python.exe. The old win32 Ruby distribution I have downloaded has rubyw.exe, which should do the same thing.

一个警告是,如果您正在运行控制台模式程序(许多脚本解释器在其中运行),则批处理文件将等待该程序退出,而使用start将产生一个新的控制台窗口。 在这种情况下,您需要做的是运行基于Windows版本的解释器,而不是基于控制台的解释器-无需start 。 对于Perl,您将运行wperl.exe而不是perl.exe 。 对于Python,它是pythonw.exe而不是python.exe 。 我下载的旧版win32 Ruby发行版包含rubyw.exe ,它应该执行相同的操作。

A final possibility is to use a 3rd-party tool to run the command prompt with a hidden window. I’ve heard of such things but never had a use for them, so I don’t know of anything in particular to point you to.

最后一种可能性是使用第三方工具来运行带有隐藏窗口的命令提示符。 我听说过这类事情,但从未使用过,所以我对您没有特别的了解。

Readers also pointed him to another SuperUser thread that highlights how you can use a Visual Basic Script to go beyond minimizing the visibility and outright hide the CMD prompt. In that thread, Harry MC explains:

读者还向他指出了另一个SuperUser线程,该线程着重强调了如何使用Visual Basic脚本来超越最小化可见性并完全隐藏CMD提示。 在该主题中,Harry MC解释了:

Solution 1:

解决方案1:

Save this one line of text as file invisible.vbs:

将这一行文本保存为文件invisible.vbs

CreateObject(“Wscript.Shell”).Run “””” & WScript.Arguments(0) & “”””, 0, False

CreateObject(“ Wscript.Shell”)。运行“””和WScript.Arguments(0)和“””,0,False

To run any program or batch file invisibly, use it like this:

要隐式运行任何程序或批处理文件,请按以下方式使用它:

wscript.exe “C:\Wherever\invisible.vbs” “C:\Some Other Place\MyBatchFile.bat”

wscript.exe“ C:\ Weverever \ invisible.vbs”“ C:\ Some Other Place \ MyBatchFile.bat”

To also be able to pass-on/relay a list of arguments use only two double quotes

为了也能够传递/中继参数列表,请仅使用两个双引号

CreateObject(“Wscript.Shell”).Run “” & WScript.Arguments(0) & “”, 0, False

CreateObject(“ Wscript.Shell”)。运行“”和WScript.Arguments(0)和“”,0,否

eg: Invisible.vbs “Kill.vbs ME.exe”

例如:Invisible.vbs“ Kill.vbs ME.exe”

Solution 2:

解决方案2:

Use a command line tool to silently launch a process : Quiet.

使用命令行工具以静默方式启动进程:安静

Employing any of the above solutions, based on your comfort level using VBS and third party tools or not, will at minimum reduce the visibility of the CMD window or outright remove it.

根据您的舒适度(是否使用VBS和第三方工具)采用上述任何解决方案,都将至少降低CMD窗口的可见性或将其完全删除。



Have something to add to the explanation? Sound off in the the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.

有什么补充说明吗? 在评论中听起来不对。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/131597/can-i-run-a-windows-batch-file-without-a-visible-command-prompt/

命令提示符运行文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值