Keep CMD open after BAT file executes

2019-01-10 06:58发布

问题:

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 it's self.

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

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

回答1:

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

cmd /k my_script.bat


回答2:

Put pause at the end of your .BAT file.



回答3:

just Add @pause at the end

Example :

@echo off
ipconfig
@pause

or u can also use :

cmd /k ipconfig


回答4:

Just to clarify something that took me a little bit to understand after reading these responses...

You have to type (literally) "cmd /k" followed by your command. I, at first, thought that "cmd" was to be replaced by your command. Not the case!

For instance I was trying to run java command...

cmd /k java myPackage.myClass

It kept the window open so I could see my "System.out.println()" messages.

I hope this clarification saves someone the approximately 3.5 minutes it took me to figure it out.



回答5:

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


回答6:

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!



回答7:

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



回答8:

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



回答9:

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