56
16
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:
- Putting a
pause
line after batch programs to prompt the user to press a key before exiting - Running these batch files or other command line manipulating tools (even service starting, restarting, etc. with
net start xy
or anything similar) withincmd.exe
(Start - Run - cmd.exe) - Running these programs with
cmd /k
like this:cmd /k myprogram.bat
But there are some other cases in which the user:
- 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
- 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?
1I know this is very old, so I'll post a comment rather than an answer. The command you're looking for is "EXIT /B", which "will exit only the script (or subroutine) but not CMD.EXE". If you end your batch file with that command, the terminal window will stay open. – KlaymenDK – 2016-01-06T09:12:30.920
Related; note the different constraint from the asker: https://stackoverflow.com/questions/886848/how-to-make-windows-batch-file-pause-when-double-clicked
– aff – 2019-06-25T10:23:37.05710Your 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 – 2011-07-04T15:40:38.670
1+1 Agreed. A little to verbose, please reconsider your question and, if possible, use an example and compose a more succinct question. – slotishtype – 2011-07-04T15:47:59.863
2This 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 – 2011-07-04T16:16:15.277
@Breakthrough: The purpose of many commands is to provide information, which is why I usually tell the user to type "Cmd" first and then "enter the needed commands in the big black box with the tiny blinking cursor" and read the output back to me. "IPCONFIG" is one such command. – Randolf Richardson – 2011-07-04T16:50:40.217
1In 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... – Tamara Wijsman – 2011-07-04T17:21:16.700
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 – 2011-07-04T18:55:19.9801@Breakthrough: By the same token, end users should never see blue screens, ever. The reality is that most people using computers don't have I.T. departments, and in many companies that do have I.T. staff, they're so small that the I.T. departments don't have these types of systematic approaches/policies in place. – Randolf Richardson – 2011-07-05T05:36:13.433