How to automatically close cmd window after batch file execution?

Batch FileCmd

Batch File Problem Overview


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.

Batch File Solutions


Solution 1 - Batch File

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.

Solution 2 - Batch File

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

Solution 3 - Batch File

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

Solution 4 - Batch File

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.

Solution 5 - Batch File

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

Solution 6 - Batch File

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.

Solution 7 - Batch File

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).

Solution 8 - Batch File

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.

Solution 9 - Batch File

i do something like below

start ""
** code
exit

it's work for me

Solution 10 - Batch File

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).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyiweiView Question on Stackoverflow
Solution 1 - Batch FileAhmadView Answer on Stackoverflow
Solution 2 - Batch FileAdrian747View Answer on Stackoverflow
Solution 3 - Batch FileIo-oIView Answer on Stackoverflow
Solution 4 - Batch FileJenna LeafView Answer on Stackoverflow
Solution 5 - Batch FilescrappyView Answer on Stackoverflow
Solution 6 - Batch Filextian170174View Answer on Stackoverflow
Solution 7 - Batch FiletreacleView Answer on Stackoverflow
Solution 8 - Batch FileFloverOweView Answer on Stackoverflow
Solution 9 - Batch FileSafuhView Answer on Stackoverflow
Solution 10 - Batch FileSalmaanView Answer on Stackoverflow