7

I run batch files and they exit immediately. I dont want that to happen so that i can see my output. Can someone tell me how to make this happen ?

I use windows 7.

1
  • Up vote the answer which i selected. It is much better and more detailed than the answer by Macke. Thanks
    – gregnorm
    Commented Jun 12, 2013 at 19:39

3 Answers 3

8

Put this on the very last line of the Batch:

cmd /k
2
  • For someone who isn't on a Windows machine right now, what does this do?
    – l0b0
    Commented Nov 12, 2019 at 0:24
  • CMD /K Run Command and then return to the CMD prompt. This is useful for testing, to examine variables
    – LeasMaps
    Commented Apr 28, 2022 at 5:29
7

Adding pause is a good answer. Here are some other ways as well..

Rather than double-clicking on them to execute you can run from a command line:

  1. Press the windows key + r (this opens the "run" window)
  2. Type: cmd into the text input and press enter (or click ok)
  3. Change to the directory that contains the batch file, e.g: cd c:\scripts\foo
  4. Execute the batch file by typing it's name and pressing enter, e.g: somename.bat

If there is a lot of output and it scrolls off the screen you can direct the output to a text file instead like so:

somename.bat > output_filename.txt

Then you can open the 'output_filename.txt' file in any text editor to view/search all of the output. This is better than pause when there more output than what is available in the scrollback.

1
  • somename.bat > output_filename.txt - I saw that the output goes to a file, but the cmd keeps on blinking. I got confused if the execution had really ended or not.
    – gregnorm
    Commented Jun 12, 2013 at 19:28
6

Add the pause command at the end of your batch file. This waits for you to key something in.

(The nice thing is that if you're running the batch file from a non-interactive process, such as a automated build system or scheduled task, the pause is simply skipped.)

The help message for pause is:

C:\>help pause
Suspends processing of a batch program and displays the message
    Press any key to continue . . .

If there is lots of output and you can't scroll far enough back, adjust the screen buffer height of the command window. This can be done via right-click on the c:\ icon go to properties -> layout:

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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