How to prevent the command prompt from closing after execution?

I have a solution which can only apply on .cmd and .bat files:

Open regedit and go to each one of:

[HKEY_CLASSES_ROOT\batfile\shell\open\command]
[HKEY_CLASSES_ROOT\cmdfile\shell\open\command] 

Now change the "Default key value" to cmd.exe /k "%1" %*. Now, every batch script window will stay open after its execution.

Note that this is like using cmd.exe /c cmd.exe /k program.bat, meaning that another CMD instance will be launched into the parent one. I couldn't find how to overwrite the first /c argument.

You can also do so with [exefile], but now it'll show an empty console box if the executable has a GUI.


Quote Microsoft:

A console is closed when the last process attached to it terminates or calls FreeConsole.

In other words, Win32 console window will always be closed when the last program running inside it exits, and you cannot change this.


(For 16-bit MS-DOS programs, Windows offers an option "Close on exit" in the properties dialog, but this is not an exception to the behavior above: it's the NTVDM process that holds the window open. Also, this option is again per-program.)


Just open a command prompt at the location of your batch file, and manually key in the name of your batch file to run it within that window.

1.Navigate to the folder where your executable resides
2.Shift-Right click and select "Command Window from here"
3.type in the name of the executable and hit enter
4.The process should run, but the window should stay open


cmd.exe /k will give you trouble with some batch. If the batch exit 1 (without /B) your console will be closed. A trick is to use:

cmd.exe /k cmd /c ...

To use this by default for all batch files, set HKEY_CLASSES_ROOT\cmdfile\shell\open\command\(default) and HKEY_CLASSES_ROOT\batfile\shell\open\command\(default) to "%windir%\system32\cmd.exe" /k "%windir%\system32\cmd" /c "%1" %*.