My problem is that in Windows, there are command line windows that close immediately after execution. To solve this, I want the default behavior to be that the window is kept open. Normally, this behavior can be avoided with three methods that come to my mind:

  1. Putting a pause line after batch programs to prompt the user to press a key before exiting
  2. Running these batch files or other command line manipulating tools (even service starting, restarting, etc. with net start xy or anything similar) within cmd.exe(Start - Run - cmd.exe)
  3. Running these programs with cmd /k like this: cmd /k myprogram.bat

But there are some other cases in which the user:

  1. Runs the program the first time and doesn't know that the given program will run in Command Prompt (Windows Command Processor) e.g. when running a shortcut from Start menu (or from somewhere else), OR
  2. Finds it a little bit uncomfortable to run cmd.exe all the time and doesn't have the time/opportunity to rewrite the code of these commands everywhere to put a pause after them or avoid exiting explicitly.

I've read an article about changing default behavior of cmd.exe when opening it explicitly, with creating an AutoRun entry and manipulating its content in these locations:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor\AutoRun

(The AutoRun items are _String values_...)

I put cmd /d /k as a value of it to give it a try, but this didn't change the behaviour of the stuffs mentioned above at all... It just changed the behaviour of the command line window when opening it explicitly (Start-Run-cmd.exe).

So how does it work? Can you give me any ideas to solve this problem?

link|improve this question

50% accept rate
6  
Your question with all the explanations made my head want to explode. Can you say something like: I want to run a batch file with this "example" command, and I want the command box to stay open when finished. Is that what you are asking? Please put in a close sample of the code. – KCotreau Jul 4 '11 at 15:40
1  
+1 Agreed. A little to verbose, please reconsider your question and, if possible, use an example and compose a more succinct question. – slotishtype Jul 4 '11 at 15:47
2  
This is normal behaviour, and while your question certainly is valid, you should be questioning why you need to keep the command prompt open after (most utilities are created with this caveat in mind). – Breakthrough Jul 4 '11 at 16:16
1  
In my opinion, this is not a real question. As you just instruct the user to use a batch file instead. If that's not the intent of the question, please clarify your question by removing any confusing information and explain what your real goal is. It's hard to come up with the right answer if it's unclear... – Tom Wijsman Jul 4 '11 at 17:21
1  
@Randolf Richardson, that's because those tools aren't supposed to be seen by an end-user ever (after all, that's why we use GUIs now). If you did need such information, most IT groups will send out batch files to gather them the appropriate system information and display it. I can see no possible way that if a solution to this question is found, it will cause less problems then it solves - so many things are sent to stdout on a constant basis which were never meant to be visible for a good reason. – Breakthrough Jul 4 '11 at 18:55
show 2 more comments
feedback

2 Answers

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

link|improve this answer
7  
+1 for understanding how and why console windows close (as opposed to most who just say "OMG WHY CAN'T IT STAY OPEN!!!!!!!!!!!1111one"). It's behaving as designed, and it should stay that way IMHO. This is the primary reason why we have standard streams and batch files in the first place. – Breakthrough Jul 4 '11 at 19:29
feedback

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

Goto HKCR\cmdfile\shell\open\command and change the Default key value with cmd.exe /k "%1" %*

do so with HKCR\batfile\shell\open\command, and now every batch script window will stay open after it's execution.

Note this is like using "cmd.exe /c cmd.exe /k program.bat", meaning, 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 comes too dirty, it will shows an empty console box if the executable have a GUI.

Hope this helps

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.