20

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.

wonea
  • 1,877
Mythics
  • 300

8 Answers8

9

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

Mxx
  • 2,889
7

You need to use the /k switch when calling.

Useful details here.

arundevma
  • 1,524
Shevek
  • 16,738
7

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.

Nikita
  • 71
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?

music2myear
  • 49,799
0
pause>nul

this should work.

Answer
  • 1
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)

JinSnow
  • 912
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

0

A response from @Shevek should be the accepted answer, as my batch would run fine from cmd and two python IDEs, but error from Windows Scheduled Task and I could not troubleshoot as cmd window closes.

To elaborate, if running a batch through Windows Scheduled Task for example, it would close anyway whether using pause or not as others pointed out. The trick here is to run cmd.exe from batch using the /k switch. For example:

this .bat code will close cmd window:

"C:\anaconda\python.exe" "C:\code\nextday\main.py"

but this one will leave it open using \k switch:

start cmd.exe /k ""C:\anaconda\python.exe" "C:\code\nextday\main.py""
gregV
  • 264
  • 2
  • 5