'Keep CMD open after BAT file executes

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?



Solution 1:[1]

Put pause at the end of your .BAT file.

Solution 2:[2]

Just add @pause at the end.

Example:

@echo off
ipconfig
@pause

Or you can also use:

cmd /k ipconfig

Solution 3:[3]

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

Solution 4:[4]

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

Solution 5:[5]

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!

Solution 6:[6]

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

Solution 7:[7]

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

Solution 8:[8]

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.

Solution 9:[9]

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

Solution 10:[10]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ElGavilan
Solution 2 ZygD
Solution 3
Solution 4 SUB-HDR
Solution 5 robinCTS
Solution 6 Julito Sanchis
Solution 7
Solution 8 RustyCake
Solution 9
Solution 10 sommmen