204

For the moment my batch file look like this:

myprogram.exe param1

The program starts but the DOS Window remains open. How can I close it?

ChrisM
  • 1,576
  • 6
  • 18
  • 29
Mister Dev
  • 10,021
  • 12
  • 41
  • 33

11 Answers11

298

Use the start command to prevent the batch file from waiting for the program. Just remember to put a empty double quote in front of the program you want to run after "Start". For example, if you want to run Visual Studio 2012 from a batch command:

Start ""  "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"

notice the double quote after start.

Marshall
  • 2,981
  • 1
  • 12
  • 2
  • 58
    @SharkAlley The proper format is `start ` – Kruug Sep 25 '13 at 20:37
  • 6
    This is the correct answer. Thanks, the empty double quotes are exactly what I needed. – Richard Oct 15 '15 at 19:44
  • I have a backup script that I run weekly and would modify it ever so slightly every week, trying to get it to a) close my program, b) backup my data, and c) restart the program and close command prompt. This finally got command prompt to close (also prevented my next backup from triggering, I had to close that command prompt!!!). – NobleUplift Apr 23 '16 at 20:13
  • @NobleUplift: For me this worked,.. below is the exact line, Start "" "C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.5\bin\idea64.exe" & – Xinus Jan 02 '18 at 07:35
  • Worked exactly for me :) Start "" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe" – Shakeer Mirza Dec 27 '22 at 09:45
175

You can use the exit keyword. Here is an example from one of my batch files:

start myProgram.exe param1
exit
Daniel F. Thornton
  • 3,687
  • 2
  • 28
  • 41
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
  • 18
    Note that this will do not nice things if you are using the console interactively instead of just double-clicking on a batch file. Generally there is little to no need to ever put `exit` into a batch file. – Joey Apr 19 '11 at 07:34
  • 28
    I tried this command in Win8. I'm not sure if it's different, but note that you must provide it with a Window title as your first parameter. When I would run it the way Patrick described, it would just open a new command prompt with "myProgram.exe" as the window title: `start "VPN" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"` – Adam Plocher May 01 '13 at 06:39
  • 9
    Note that if your command contains spaces and you put it into quotes, you must add an extra quoted parameter before it, as START interprets the first quoted parameter as window name and only the folowing second parameter as command name. – David Balažic Apr 12 '17 at 14:51
55

Look at the START command, you can do this:

START rest-of-your-program-name

For instance, this batch-file will wait until notepad exits:

@echo off
notepad c:\test.txt

However, this won't:

@echo off
start notepad c:\test.txt
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
34

From my own question:

start /b myProgram.exe params...

works if you start the program from an existing DOS session.

If not, call a vb script

wscript.exe invis.vbs myProgram.exe %*

The Windows Script Host Run() method takes:

  • intWindowStyle : 0 means "invisible windows"
  • bWaitOnReturn : false means your first script does not need to wait for your second script to finish

Here is invis.vbs:

set args = WScript.Arguments
num = args.Count

if num = 0 then
    WScript.Echo "Usage: [CScript | WScript] invis.vbs aScript.bat <some script arguments>"
    WScript.Quit 1
end if

sargs = ""
if num > 1 then
    sargs = " "
    for k = 1 to num - 1
        anArg = args.Item(k)
        sargs = sargs & anArg & " "
    next
end if

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run """" & WScript.Arguments(0) & """" & sargs, 0, False
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    +1 There's got to be an easier way, but this is the only answer that worked for me. – D.N. Mar 06 '12 at 18:49
22

This is the only thing that worked for me when I tried to run a java class from a batch file:

start "cmdWindowTitle" /B "javaw" -cp . testprojectpak.MainForm

You can customize the start command as you want for your project, by following the proper syntax:

Syntax
      START "title" [/Dpath] [options] "command" [parameters]

Key:
   title      : Text for the CMD window title bar (required)
   path       : Starting directory
   command    : The command, batch file or executable program to run
   parameters : The parameters passed to the command

Options:
   /MIN       : Minimized
   /MAX       : Maximized
   /WAIT      : Start application and wait for it to terminate
   /LOW       : Use IDLE priority class
   /NORMAL    : Use NORMAL priority class
   /HIGH      : Use HIGH priority class
   /REALTIME  : Use REALTIME priority class

   /B         : Start application without creating a new window. In this case
                ^C will be ignored - leaving ^Break as the only way to 
                interrupt the application
   /I         : Ignore any changes to the current environment.

   Options for 16-bit WINDOWS programs only

   /SEPARATE   Start in separate memory space (more robust)
   /SHARED     Start in shared memory space (default)
Zosimas
  • 518
  • 4
  • 15
20

You should try this. It starts the program with no window. It actually flashes up for a second but goes away fairly quickly.

start "name" /B myprogram.exe param1
Chris Dail
  • 25,715
  • 9
  • 65
  • 74
  • 4
    The `"title"` option is important. If path of the program includes spaces it has to be enclosed by quotes thus we have to add `"title"` to avoid failure. – A.D. Jan 12 '13 at 13:50
  • 1
    If you close the main window, the background process 'myprogram.exe' closes too. – YetAnotherBot Feb 04 '19 at 13:23
16

How to solve "space problem" and local dependencies:

@echo off
cd "C:\Program Files\HeidiSQL"
start heidisql.exe

cd "C:\Program Files (x86)\Google\Chrome\Application"
start chrome.exe

exit
Gilco
  • 1,326
  • 12
  • 13
7

Loads of answers for this question already, however I am posting this to highlight something important:

Start "C:\Program Files\someprog.exe"

The above might cause issues in some windows versions as Start actually expects the first set of quotation marks to be a windows title. So it is best practice to first double quote a comment, or a blank comment:

Start "" "C:\Program Files\someprog.exe"

or

Start "Window Title" "C:\Program Files\someprog.exe"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • 1
    This made my day. Now it actually works, otherwise it would only give me the Open With dialog when trying to open a file with exe. – Henrik Oct 18 '20 at 00:48
1

My solution to do this from the GUI:

  1. Create a shortcut to the program you want to run;

  2. Edit the shortcut's properties;

  3. Change the TARGET field to %COMSPEC% /C "START "" "PROGRAMNAME"";

  4. Change the RUN field to minimized.

Ready! See how you like it...

PS: Program parameters can be inserted in between the two final quotation marks; the PROGRAMNAME string can be either a filename, a relative or an absolute path -- if you put in an absolute path and erase the drive letter and semicolon, then this will work in a thumbdrive no matter what letter the host computer assigns to it... (also, if you place the shortcut in the same folder and precede the program filename in PROGRAMNAME with the %CD% variable, paths will always match; same trick can be used in START IN field).

Community
  • 1
  • 1
Midas
  • 21
  • 2
0

If this batch file is something you want to run as scheduled or always; you can use windows schedule tool and it doesn't opens up in a window when it starts the batch file.

To open Task Scheduler:

  • Start -> Run/Search -> 'cmd'
  • Type taskschd.msc -> enter

From the right side, click Create Basic Task and follow the menus.

Hope this helps.

Leustad
  • 143
  • 3
  • 13
0

Here is my preferred solution. It is taken from an answer to a similar question.

Use a VBS Script to call the batch file:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\path\to\your\batchfile.bat" & Chr(34), 0
Set WshShell = Nothing

Copy the lines above to an editor and save the file with .VBS extension.

Community
  • 1
  • 1
Ilyich
  • 4,966
  • 3
  • 39
  • 27