如何从我的 bat 文件中最小化命令提示符 [英] How do I minimize the command prompt from my bat file
问题描述
我有这个 bat 文件,我想在运行它时最小化 cmd 窗口:
I have this bat file and I want to minimize the cmd window when I run it:
@echo off
cd /d C:leadsssh
call C:Ruby192insetrbvars.bat
ruby C:leadssshput_leads.rb
基本上我希望命令窗口立即最小化.关于如何做到这一点的任何想法?
Basically I want the command window minimized immediately. Any ideas on how to do this?
推荐答案
有一种非常有趣的方式来执行最小化脚本,让他重新启动自身最小化.这是放在脚本开头的代码:
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
工作原理
当脚本正在执行时 IS_MINIMIZED
未定义(if not DEFINED IS_MINIMIZED
)所以:
- IS_MINIMIZED 设置为 1:
设置 IS_MINIMIZED=1
. 脚本使用start命令
&&启动自身的副本开始 ""/min "%~dpnx0" %*
其中:
- IS_MINIMIZED is set to 1:
set IS_MINIMIZED=1
. Script starts a copy of itself using start command
&& start "" /min "%~dpnx0" %*
where:
""
- 窗口的空标题./min
- 切换到最小化运行."%~dpnx0"
- 脚本的完整路径.%*
- 传递所有脚本的参数.
""
- empty title for the window./min
- switch to run minimized."%~dpnx0"
- full path to your script.%*
- passing through all your script's parameters.
然后初始脚本完成它的工作:&&退出
.
对于脚本变量的启动副本IS_MINIMIZED
由原始脚本设置,因此它只是跳过第一行的执行并直接进入脚本逻辑.
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.
- 您必须保留一些变量名称才能将其用作标志.
- 脚本应该以
exit
结束,否则脚本执行后cmd窗口不会关闭. 如果您的脚本不接受参数,您可以使用参数作为标志而不是变量:
- 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 &&退出
或更短if "%1" == "" start ""/min "%~f0" MY_FLAG &&退出
这篇关于如何从我的 bat 文件中最小化命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!