1. 程式人生 > 實用技巧 >命令提示符執行檔案_我可以在沒有可見命令提示符的情況下執行Windows批處理檔案嗎?...

命令提示符執行檔案_我可以在沒有可見命令提示符的情況下執行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 youcando 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 thestartcommand. By default,startreturns 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 usingstartwill 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 — nostartnecessary. For Perl, you would runwperl.exeinstead ofperl.exe. For Python, it’spythonw.exeinstead ofpython.exe. The old win32 Ruby distribution I have downloaded hasrubyw.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 fileinvisible.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/

命令提示符執行檔案