windows 如何从我的 bat 文件中最小化命令提示符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9232308/
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
How do I minimize the command prompt from my bat file
提问by Matt Elhotiby
I have this bat file and I want to minimize the cmd window when I run it:
我有这个 bat 文件,我想在运行它时最小化 cmd 窗口:
@echo off
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb
Basically I want the command window minimized immediately. Any ideas on how to do this?
基本上我希望命令窗口立即最小化。关于如何做到这一点的任何想法?
回答by izzekil
There is a quite interesting way to execute script minimized by making him restart itself minimised. Here is the code to put in the beginning of your script:
有一种非常有趣的方式来执行最小化脚本,让他重新启动自身最小化。这是放在脚本开头的代码:
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
... script logic here ...
exit
How it works
这个怎么运作
When the script is being executed IS_MINIMIZED
is not defined (if not DEFINED IS_MINIMIZED
) so:
当脚本被执行时IS_MINIMIZED
没有定义 ( if not DEFINED IS_MINIMIZED
) 所以:
- IS_MINIMIZEDis set to 1:
set IS_MINIMIZED=1
. Script starts a copy of itself using startcommand
&& start "" /min "%~dpnx0" %*
where:""
- empty title for the window./min
- switch to run minimized."%~dpnx0"
- full path to your script.%*
- passing through all your script's parameters.
Then initial script finishes its work:
&& exit
.
- IS_MINIMIZED设置为1:
set IS_MINIMIZED=1
。 脚本使用start命令 启动其自身的副本,
&& start "" /min "%~dpnx0" %*
其中:""
- 窗口的空标题。/min
- 切换到最小化运行。"%~dpnx0"
- 脚本的完整路径。%*
- 传递所有脚本的参数。
然后初始脚本完成其工作:
&& exit
.
For the started copy of the script variable IS_MINIMIZED
is set by the original script so it just skips the execution of the first line and goes directly to the script logic.
由于脚本变量的启动副本IS_MINIMIZED
由原始脚本设置,因此它只是跳过第一行的执行并直接进入脚本逻辑。
Remarks
评论
- You have to reserve some variable name to use it as a flag.
- The script should be ended with
exit
, otherwise the cmd window wouldn't be closed after the script execution. If your script doesn't accept arguments you could use argument as a flag instead of variable:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
or shorterif "%1" == "" start "" /min "%~f0" MY_FLAG && exit
- 您必须保留一些变量名称才能将其用作标志。
- 脚本应该以 结束
exit
,否则脚本执行后 cmd 窗口不会关闭。 如果您的脚本不接受参数,您可以使用参数作为标志而不是变量:
if "%1" == "" start "" /min "%~dpnx0" MY_FLAG && exit
或更短if "%1" == "" start "" /min "%~f0" MY_FLAG && exit
回答by Cody Gray
Use the start
command, with the /min
switch to run minimized. For example:
使用start
命令,用/min
开关最小化运行。例如:
start /min C:\Ruby192\bin\setrbvars.bat
Since you've specified a batch file as the argument, the command processor is run, passing the /k
switch. This means that the window will remain on screen after the command has finished. You can alter that behavior by explicitly running cmd.exe
yourself and passing the appropriate switches if necessary.
由于您已将批处理文件指定为参数,因此运行命令处理器并传递/k
开关。这意味着命令完成后窗口将保留在屏幕上。您可以通过显式运行cmd.exe
自己并在必要时传递适当的开关来改变该行为。
Alternatively, you can create a shortcut to the batch file (are PIF files still around), and then alter its properties so that it starts minimized.
或者,您可以创建批处理文件的快捷方式(PIF 文件仍然存在),然后更改其属性,使其开始最小化。
回答by Bruno Silva
The only way I know is by creating a Windows shortcut to the batch file and then changing its properties to run minimized by default.
我知道的唯一方法是创建批处理文件的 Windows 快捷方式,然后更改其属性以在默认情况下最小化运行。
回答by user1cat
Using PowerShell you can minimize from the same file without opening a new instance.
使用 PowerShell,您可以在不打开新实例的情况下最小化同一文件。
powershell -window minimized -command ""
Also -window hidden
and -window normal
is available to hide completely or restore.
另外-window hidden
和-window normal
可完全隐藏或恢复。
回答by Peter T.
If you want to start the batch for Win-Run / autostart, I found I nice solution here https://www.computerhope.com/issues/ch000932.htm& https://superuser.com/questions/364799/how-to-run-the-command-prompt-minimized
如果您想为 Win-Run / 自动启动启动批处理,我在这里找到了不错的解决方案https://www.computerhope.com/issues/ch000932.htm& https://superuser.com/questions/364799/how-最小化运行命令提示
cmd.exe /c start /min myfile.bat ^& exit
- the
cmd.exe
is needed as start is no windows command that can be executed outside a batch /c
= exit after the start is finished- the
^& exit
part ensures that the window closes even if the batch does not end withexit
- 在
cmd.exe
需要为开始是没有窗户命令可以分批外面来执行 /c
= 启动完成后退出- 该
^& exit
部件确保窗口关闭,即使批次没有以exit
However, the initial cmd is still not minimized.
但是,初始 cmd 仍然没有最小化。
回答by Kyroflash
If you type this text in your bat file:
如果您在 bat 文件中键入此文本:
start /min blah.exe
It will immediately minimize as soon as it opens the program. You will only see a brief flash of it and it will disappear.
打开程序后,它会立即最小化。您只会看到它的短暂闪光,然后它就会消失。
回答by SSi
One way to 'minimise' the cmd window is to reduce the size of the console using something like...
“最小化” cmd 窗口的一种方法是使用类似...
echo DO NOT CLOSE THIS WINDOW
MODE CON COLS=30 LINES=2
You can reduce the COLS to about 18 and the LINES to 1 if you wish. The advantage is that it works under WinPE, 32-bit or 64-bit, and does not require any 3rd party utility.
如果您愿意,您可以将 COLS 减少到大约 18,将 LINES 减少到 1。优点是它可以在 WinPE、32 位或 64 位下运行,并且不需要任何 3rd 方实用程序。
回答by Bernard Chen
One option is to find one of the various utilities that can change the window state of the currently running console window and make a call to it from within the batch script.
一种选择是找到可以更改当前运行的控制台窗口的窗口状态的各种实用程序之一,并从批处理脚本中调用它。
You can run it as the first thing in your batch script. Here are two such tools:
您可以将它作为批处理脚本中的第一件事来运行。这里有两个这样的工具:
min.exe http://www.paulsadowski.com/wsh/cmdprogs.htm
min.exe http://www.paulsadowski.com/wsh/cmdprogs.htm
回答by robert4
Yet another free 3rd party tool that is capable of minimizing the console window at any time (not only when starting the script) is Tcl with the TWAPI extension:
另一个免费的 3rd 方工具可以随时(不仅是在启动脚本时)最小化控制台窗口,它是带有 TWAPI 扩展的 Tcl:
echo package require twapi;twapi::minimize_window [twapi::get_console_window] | tclkitsh -
here tclkitsh.exe
is in the PATH and is one of the tclkit-cli-*-twapi-*.exe
files downloadable from sourceforge.net/projects/twapi/files/Tcl binaries/Tclkits with TWAPI/. I prefer it to the much lighter min.exe
mentioned in Bernard Chen's answerbecause I use TWAPI for countless other purposes already.
这里tclkitsh.exe
位于 PATH 中,是tclkit-cli-*-twapi-*.exe
可从sourceforge.net/projects/twapi/files/Tcl binaries/Tclkits下载的文件之一,带有 TWAPI/。我更喜欢它而不是Bernard Chen 的回答中min.exe
提到的更轻松,因为我已经将 TWAPI 用于无数其他目的。
回答by npocmaka
You can minimize the command prompt on during the run but you'll need two additional scripts: windowModeand getCmdPid.bat:
您可以在运行期间最小化命令提示符,但您需要两个额外的脚本:windowMode和getCmdPid.bat:
@echo off
call getCmdPid
call windowMode -pid %errorlevel% -mode minimized
cd /d C:\leads\ssh
call C:\Ruby192\bin\setrbvars.bat
ruby C:\leads\ssh\put_leads.rb