18

I know there were some tricks with older versions of windows to accomplish this, but not sure on windows 7.

If I had a piece of software that executes a command with switches resulting in a cmd.exe window opening and closing quickly, how can I reconfigure the command prompt to NOT auto-close once the task is complete.

The trick here is that the command being executed is indeed a batch file, includes a pause statement at the end, but the way the software is trying to execute the command is failing. Since it closes so quickly, we don't know what error is being returned since it closes so quickly.

  • Good question, quietly I also wished I known this because sometimes game/software patches come in .BAT files closing after instructions are executed without pausing and displaying executed commands to end user. – Boris_yo May 23 '12 at 15:53
  • 1
    Why not open a command window, cd to that directory and run the batch from there? That would run in the same non-closing cmd window that would stay open when the batch finishes. That might give you some clues to help debug it. – jdh May 23 '12 at 16:00
  • 3
    possible duplicate of How to prevent the command prompt from closing after execution? – slhck May 23 '12 at 16:02
  • Thanks slhck, you pointing me to that location looks like the closest possibility to achieve what I want is within the registry for batfile/cmdfile. – Mythics May 23 '12 at 17:28
8

Instead of running that .bat file directly from Explorer or whatever other method you use, manually launch cmd.exe(if needed with elevated privileges) and then run your .bat file from there. Then upon completion of its execution you will still see all the output...just like in the good old DOS days. :)

  • Press start and type cmd and press enter, you will launch a command prompt. Just drag and drop what you need to run (your python script or .exe) into the cmd windows, and press enter. – JinSnow Apr 17 '17 at 6:01
6

In your .bat file, use the pause command. It will prevent your .bat file from closing.

For example, this is my bat file I use to start MySQL service on my PC:

net start MySQL
pause

Note: On Windows 7 I start this .bat file from Administrator's name. To do this click right mouse's button on your .bat file and chose "Start from Administrator's name" from the dropdown menu.

5

You need to use the /k switch when calling.

Useful details here.

1

In .bat scripting, you leave off the "exit" at the end of the script to leave the command window open.

A command window opened by a script always closes itself at the end of the script because it is dependent on the script. Opening the window yourself and running the script does not close the window unless you've specific the "exit" command at the end of the script because the window is not dependent on the script.

It does not appear there is a way to prevent a script-dependent window from being closed once the script ends in Windows 7. However, it is possible error messages would be recorded elsewhere. Does the program have a log file? Or are there entries in the Windows logs?

0
pause>nul

this should work.

  • 2
    Please read the question again carefully. Your answer does not answer the original question. "The trick here is that the command being executed is indeed a batch file, includes a pause statement at the end," – DavidPostill Apr 28 '16 at 8:08
0

Press start and type cmd and press enter, you will launch a command prompt. Just drag and drop what you need to run (your python script, .exe ...) into the cmd windows, and press enter. (If you need to run the cmd as admin, find the cmd in the start menu, right click on it, choose run as admin) (Works on windows 7)

  • Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill Apr 17 '17 at 8:58
0

I was having this problem for a while now and I finally realized what is happening.

When you start a task with task scheduler and it's a bat file, it'll open the cmd window to C:\WINDOWS\system32 and NOT where the bat file is located. Why? I have no clue! So what I had to do is add the directory with cd into the bat file.

Example below:

cd c:\blahblahblah

startmyprogram.exe

  • Your answer creates lots of confusion and you are unable to establish the reason behind the problem OP came across. Better you refer this link superuser.com/help/how-to-answer, will help you while answering. – Rajesh S Mar 4 '18 at 8:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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