122

I'm running a batch file that has these two lines:

start C:\Users\Yiwei\Downloads\putty.exe -load "MathCS-labMachine1"
"C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -multiwindow

This batch file is used to run the Xming application and then the PuTTY app so I can SSH into my university's computer lab.

However, if I run this and Xming is not already open, once I exit from the PuTTY terminal the cmd window remains open. Only if I have already run Xming does the cmd window close when I close the PuTTY terminal. I've tried adding exit to the last line of the batch file, but to no avail.

phuclv
  • 37,963
  • 15
  • 156
  • 475
yiwei
  • 4,022
  • 9
  • 36
  • 54
  • 1
    is `"C:\Program Files (x86)\Xming\Xming.exe"` a parameter from putty.exe, or an seperate command? – bpoiss Feb 04 '13 at 23:47
  • If the second line is a second command, try adding start before that as well. – Gray Feb 05 '13 at 00:50
  • 1
    @BernhardPoiss it is a separate command, NOT a parameter for putty.exe. If I put that line first, it hangs and doest open putty. If i put start in front of it, windows says "cannot find 'C:\Program '. please make sure it is type correctly. – yiwei Feb 05 '13 at 03:40
  • Congrats! hundredth like on this question. – Syed M. Sannan Jan 07 '22 at 16:01

12 Answers12

168

Modify the batch file to START both programs, instead of STARTing one and CALLing another

start C:\Users\Yiwei\Downloads\putty.exe -load "MathCS-labMachine1"
start "" "C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -multiwindow

If you run it like this, no CMD window will stay open after starting the program.

Ahmad
  • 12,336
  • 6
  • 48
  • 88
  • 38
    +1 except when you use double quotes for the path you have to add a first parameter of `""` as title becomes required. Second command should be `start "" "C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -multiwindow` otherwise it will try to open a command window titled `C:\Program Files (x86)\Xming\Xming.exe` attempting to execute `:0` with parameters `-clipboard -multiwindow` – Patrick Meinecke Feb 10 '13 at 00:59
  • Also, this `start /B "GCFScape" "x64\GCFScape.exe">nul` example was helpful for me – xacinay Jun 20 '14 at 13:31
  • 4
    "If command is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run." http://ss64.com/nt/start.html – CAD bloke Apr 20 '16 at 09:08
  • 4
    @CADbloke Your mentioned problem can be solved using `cmd.exe /C "start cmd.exe /C [...].bat ..."` – Thorsten Schöning Nov 22 '16 at 15:30
  • 1
    @patrick-meinecke Thanks for adding the comment about "", I was frustrated that this was causing me issues. I read you comment and an ah ha moment ensued. – Michael Eakins Dec 29 '16 at 14:12
  • Thanks alot that really helped! +1 – thebunnyrules Feb 12 '17 at 23:45
  • This does not appears to the case (anymore?). I have a single `run.bat` that has five `start someotherfile.bat` for different files, and if I run that by double clicking in a file explorer window, the cmd shell pops up, all five things run, they terminate, and then run.bat terminates leaving the cmd shell open. – Mike 'Pomax' Kamermans Jun 28 '22 at 15:38
  • start "" does the job. Thanks for the help! – flk Nov 08 '22 at 08:20
39

You normally end a batch file with a line that just says exit. If you want to make sure the file has run and the DOS window closes after 2 seconds, you can add the lines:

timeout 2 >nul
exit

But the exit command will not work if your batch file opens another window, because while ever the second window is open the old DOS window will also be displayed.

SOLUTION: For example there's a great little free program called BgInfo which will display all the info about your computer. Assuming it's in a directory called C:\BgInfo, to run it from a batch file with the /popup switch and to close the DOS window while it still runs, use:

start "" "C:\BgInfo\BgInfo.exe" /popup
exit
lcnicolau
  • 3,252
  • 4
  • 36
  • 53
Adrian747
  • 399
  • 3
  • 2
11

Closing cmd window after opening VSCode with batch script

If you have the code command which installs with VSCode:

echo | code . | exit /b

If you're running the script as administrator:

cd /d %~dp0
echo | code . | exit /b

Using start /b will usually work to allow the cmd window to close for other programs.

Quinn Dirks
  • 311
  • 3
  • 7
10

If you want to separate the commands into one command per file, you can do

cmd /c start C:\Users\Yiwei\Downloads\putty.exe -load "MathCS-labMachine1"

and in the other file, you can do

cmd /c start "" "C:\Program Files (x86)\Xming\Xming.exe" :0 -clipboard -multiwindow

The command cmd /c will close the command-prompt window after the exe was run.

Jenna Leaf
  • 2,255
  • 21
  • 29
10

To close the current cmd windows immediately, just add as the last command/line:

move nul 2>&0

Try move nul to nowhere and redirect the stderr to stdin will result in the current window cmd.exe being closed

This is different from closing a bat, or exiting it using goto :EOF or Exit /b

I even opened a question to find an answer that would explain behavior. Would that be a bug? Immediate closing of the current cmd window when executing: move nul 2>&0

Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • 1
    In my case I had to do the following "2>&0 > /dev/null &". But good idea. Thank you. – Marek Malczewski Mar 16 '21 at 07:12
  • The ONLY thing that worked...shame for all those answers suggesting exit /b or cmd /c. THANK YOU! – Billy Cao Nov 22 '21 at 01:54
  • the problem with this is that the exit code will be something less than "completed successfully". But I suppose you could account for this if the exit code is consistent. `cmd /c` before the start command seems to work, along with `exit /b` afterwards – Jon Grah Dec 02 '22 at 23:51
  • @JonGrah hi, thanks for comment, but plz read title: ***How to automatically close cmd window after batch file execution?*** See "**after batch file execution"** does not suggest that success has already been achieved in executing the bat? so I understand that all that remains is to close the window. Do you agree? Otherwise I would be changing the current question, and then yes, I would post other code in compatibility with your answer and thus focusing on adding one or more commands, not just closing the window as you proposed – Io-oI Dec 03 '22 at 13:16
2

This worked for me. I just wanted to close the command window automatically after exiting the game. I just double click on the .bat file on my desktop. No shortcuts.

taskkill /f /IM explorer.exe
C:\"GOG Games"\Starcraft\Starcraft.exe
start explorer.exe
exit /B
phuclv
  • 37,963
  • 15
  • 156
  • 475
scrappy
  • 47
  • 1
1

You could try the somewhat dangerous: taskkill /IM cmd.exe ..dangerous bcz that will kill the cmd that is open and any cmd's opened before it.

Or add a verification to confirm that you had the right cmd.exe and then kill it via PID, such as this:

set loc=%time%%random%
title=%loc%
for /f "tokens=2 delims= " %%A in ('tasklist /v ^| findstr /i "%loc%"') do (taskkill /PID %%A) 

Substitute (pslist &&A) or (tasklist /FI "PID eq %%A") for the (taskkill /PID %%A) if you want to check it first (and maybe have pstools installed).

treacle
  • 59
  • 2
1

i do something like below

start ""
** code
exit

it's work for me

Safuh
  • 87
  • 1
  • 1
0

I had this, I added EXIT and initially it didn't work, I guess per requiring the called program exiting advice mentioned in another response here, however it now works without further ado - not sure what's caused this, but the point to note is that I'm calling a data file .html rather than the program that handles it browser.exe, I did not edit anything else but suffice it to say it's much neater just using a bat file to access the main access pages of those web documents and only having title.bat, contents.bat, index.bat in the root folder with the rest of the content in a subfolder.

i.e.: contents.bat reads

cd subfolder
"contents.html"
exit

It also looks better if I change the bat file icons for just those items to suit the context they are in too, but that's another matter, hiding the bat files in the subfolder and creating custom icon shortcuts to them in the root folder with the images called for the customisation also hidden.

superpuccio
  • 11,674
  • 8
  • 65
  • 93
0

Sometimes you can reference a Windows "shortcut" file to launch an application instead of using a ".bat" file, and it won't have the residual prompt problem. But it's not as flexible as bat files.

FloverOwe
  • 302
  • 2
  • 8
0

After reading several answers, I learned a couple of things with experimentation:

  1. files ending with CMD vs BAT will be handled slightly differently.

  2. I was trying to run with silentCMD.exe but the problem is that silentCMD would always wait until any started program finished running before reporting task as being completed....even if the CMD window quit ahead of time.

  3. using move nul 2>&0 does seem to work, but the exit code is never delivered properly. so when using task scheduler, it may have weird results unless you can somehow detect the exact weird result and workaround it.

Solution was to just run the task scheduler (or bat/cmd script) directly without using silentCMD to launch it.


With .BAT file extension

@echo off

if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized

taskkill /IM "Program.exe" /T /F

start "" "C:\Program.exe" /arg1 /arg2

TIMEOUT /T 2 /NOBREAK

EXIT /B

The if not "%minimized%"=="" goto :minimized portion is to launch the bat (or cmd) minimized. difficult to get CMD window to be silent, so this is the next best thing.

BAT files seem to just exit properly without having to do anything extra.


If using .CMD file extension

@echo off

if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized

taskkill /IM "Program.exe" /T /F

cmd /c start "" "C:\Program.exe" /arg1 /arg2

TIMEOUT /T 2 /NOBREAK

EXIT /B

You must use the cmd /c before the start in order to get the script to close after the launch.

Jon Grah
  • 296
  • 2
  • 13
-1

Just try /s as listed below.

As the last line in the batch file type:

exit /s

The above command will close the Windows CMD window.

/s - stands for silent as in (it would wait for an input from the keyboard).

jtate
  • 2,612
  • 7
  • 25
  • 35
Salmaan
  • 39
  • 1
  • 2
    `cmd`'s [exit](https://ss64.com/nt/exit.html) command doesn't have a `/s` switch (and it would be pointless anyway) – Stephan Dec 07 '19 at 08:19
  • you can use `exit /any_character` to get the same result, so nothing new... try this `exit /pizza` this also do the same... – Io-oI May 19 '21 at 03:16