199

I have a bat file like this:

ipconfig

That will print out the IP info to the screen, but before the user can read that info CMD closes itself.

I believe that CMD assumes the script has finished, so it closes.

How do I keep CMD open after the script is finished?

ZygD
  • 8,011
  • 21
  • 49
  • 67
pattyd
  • 5,249
  • 10
  • 35
  • 53
  • 1
    Dupe of http://stackoverflow.com/questions/988403/stop-a-batch-file-from-autoclosing – Rob Kielty Jul 30 '13 at 20:51
  • 4
    @RobKielty it's the same question alright - but here we have a new answer (which is better IMHO) – Nir Alfasi Jul 30 '13 at 20:57
  • 1
    possible duplicate of [How to keep cmd running after opening a .bat file script](http://stackoverflow.com/questions/15012344/how-to-keep-cmd-running-after-opening-a-bat-file-script) – Endoro Jul 30 '13 at 20:58
  • @alfasin Is that not the same as Rutger Nijlunsing's answer? – Rob Kielty Jul 30 '13 at 20:59
  • @RobKielty nope, my answer isn't the same as it doesn't use `pause` which is a nice workaround (and that's why I upvoted this answer) but not one that really solves the issue because after the user hits `Enter` or `Space` the window will close... – Nir Alfasi Jul 30 '13 at 21:01

11 Answers11

268

Put pause at the end of your .BAT file.

ElGavilan
  • 5,747
  • 16
  • 24
  • 35
  • 9
    This is exactly what I was looking for, because I'm running the bat file at startup, not through a CMD command. +1 – Hawkeye Oct 28 '16 at 15:41
  • 4
    If you still want to interact with the bat file after your execution, replace the PAUSE with just cmd /k - then after your scripts have run, you can still run manual commands afterwards in the same context – Luigi D'Amico Nov 13 '18 at 14:39
  • I can't stand seeing that this was not the "solution" comment for this question – newhouse Apr 25 '19 at 10:27
188

Depending on how you are running the command, you can put /k after cmd to keep the window open.

cmd /k my_script.bat

Simply adding cmd /k to the end of your batch file will work too. Credit to Luigi D'Amico who posted about this in the comments below.

Mister SirCode
  • 799
  • 9
  • 26
Jason Schindler
  • 2,200
  • 1
  • 9
  • 8
61

Just add @pause at the end.

Example:

@echo off
ipconfig
@pause

Or you can also use:

cmd /k ipconfig
ZygD
  • 8,011
  • 21
  • 49
  • 67
12

When the .bat file is started not from within the command line (e.g. double-clicking).

echo The echoed text
@pause

at_pause

echo The echoed text
pause

pause

echo The echoed text
cmd /k

cmd k

echo The echoed text & pause

and_pause

ZygD
  • 8,011
  • 21
  • 49
  • 67
9

Adding pause in (Windows 7) to the end did not work for me
but adding the cmd /k in front of my command did work.

Example :

cmd /k gradlew cleanEclipse
SUB-HDR
  • 498
  • 1
  • 5
  • 20
Robin
  • 91
  • 1
3

start cmd /k did the magic for me. I actually used it for preparing cordova phonegap app it runs the command, shows the result and waits for the user to close it. Below is the simple example

start cmd /k echo Hello, World!

What I did use in my case

start cmd /k cordova prepare

Update

You could even have a title for this by using

start "My Title" echo Hello, World!

robinCTS
  • 5,458
  • 14
  • 27
  • 37
jafarbtech
  • 5,941
  • 1
  • 29
  • 49
1

If you are starting the script within the command line, then add exit /b to keep CMD opened

Julito Sanchis
  • 878
  • 1
  • 7
  • 9
  • Doesn't practically solve the question as modifying `ipconfig` and other built-in batch files ***is a very bad idea***. – robinCTS Nov 14 '17 at 03:22
1

In Windows add '& Pause' to the end of your command in the file.

samadadi
  • 2,250
  • 4
  • 20
  • 35
1

I was also confused as to why we're adding a cmd at the beginning and I was wondering if I had to open the command prompt first. What you need to do is type the full command along with cmd /k. For example assume your batch file name is "my_command.bat" which runs the command javac my_code.java then the code in your batch file should be:

cmd /k javac my_code.java

So basically there is no need to open command prompt at the current folder and type the above command but you can save this code directly in your batch file and execute it directly.

RustyCake
  • 29
  • 1
  • 1
  • 4
0
javac -d C:\xxx\lib\ -classpath C:\xxx\lib\ *.java

cmd cd C:\xxx\yourbat.bat

the second command make your cmd window not be closed. The important thing is you still able to input new command

0

As a sidenote this also works when running a command directly from the search bar in windows.

e.g. directly running ipconfig will directly close the cmd window after the command has exited.

enter image description here

Using cmd \k <command> won't - which was what i was trying to do when i found this answer.

enter image description here

It has the added advantage of always recognizing the command you're trying to run. E.g. running echo hello world from the searchbar won't work because that is not a command, however cmd \k echo hello world works just fine.

sommmen
  • 2,980
  • 1
  • 12
  • 23