Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I minimize the command prompt from my bat file

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?

like image 471
Matt Elhotiby Avatar asked Feb 10 '12 17:02

Matt Elhotiby


People also ask

How do I minimize command prompt?

Switch to the Command Prompt window that you want to minimize, and right-click the minimize button. What is this? This will send the window to the system tray. You will see an EXE icon in the system tray to indicate that it is running.

What does @echo off do in a batch file?

Example# @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

How do I resize a batch window?

If you just open the batch file, click on the window, and then click "properties", and then to "layout", and scroll down to "Window Size", you can edit it from there. It will also stay that way every time you open that specific batch file, so it's pretty handy.


1 Answers

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:

  1. IS_MINIMIZED is set to 1: set IS_MINIMIZED=1.
  2. Script starts a copy of itself using start command && start "" /min "%~dpnx0" %* where:

    1. "" - empty title for the window.
    2. /min - switch to run minimized.
    3. "%~dpnx0" - full path to your script.
    4. %* - passing through all your script's parameters.
  3. 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

  • 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 shorter if "%1" == "" start "" /min "%~f0" MY_FLAG && exit

like image 177
izzekil Avatar answered Sep 18 '22 15:09

izzekil