I have this bat file and I want to minimize the cmd window when I run it:
@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?
Use the
start
command, with the/min
switch to run minimized. For example: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 runningcmd.exe
yourself and passing the appropriate switches if necessary.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.
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
cmd.exe
is needed as start is no windows command that can be executed outside a batch/c
= exit after the start is finished^& exit
part ensures that the window closes even if the batch does not end withexit
However, the initial cmd is still not minimized.
You could try running a script as follows
save the file as filename.js
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
cmdow http://www.commandline.co.uk/cmdow/index.html
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:
How it works
When the script is being executed
IS_MINIMIZED
is not defined (if not DEFINED IS_MINIMIZED
) so:set IS_MINIMIZED=1
.Script starts a copy of itself using start command
&& 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
.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.Remarks
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
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:
here
tclkitsh.exe
is in the PATH and is one of thetclkit-cli-*-twapi-*.exe
files downloadable from sourceforge.net/projects/twapi/files/Tcl binaries/Tclkits with TWAPI/. I prefer it to the much lightermin.exe
mentioned in Bernard Chen's answer because I use TWAPI for countless other purposes already.