ChatGPT解决这个技术问题 Extra ChatGPT

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?

@RobKielty it's the same question alright - but here we have a new answer (which is better IMHO)
@alfasin Is that not the same as Rutger Nijlunsing's answer?
@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...

E
ElGavilan

Put pause at the end of your .BAT file.


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
M
Mister SirCode

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.


After all your scripts in your bat file - put --> cmd /k
Beware that if your program contains syntactical errors, the window will still close if you ran it via double-click and put cmd /k at the end.
This doesn't explain cmd windows closing if the user double clicked the file.
This command seems to reset my shell. If use it in a 'Git Bash' shell (a modified cmd prompt that came with my git installation) on windows, the the command prompt is reverted to the standard version and won't remember the the commands I typed in... Is there a way to get around this?
Z
ZygD

Just add @pause at the end.

Example:

@echo off
ipconfig
@pause

Or you can also use:

cmd /k ipconfig

what does the @ symbol do? Is there a difference between @pause and pause?
@demonicdaron Answer explaining the @ symbol: stackoverflow.com/a/21077142/2258
But in this case, it should not make any difference.
The difference between @pause and pause can be seen visually in this answer: stackoverflow.com/questions/17957076#60053224
Here, it does not make any difference as "echo" is already set to "off".
Z
ZygD

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

echo The echoed text
@pause

https://i.stack.imgur.com/JTmOR.jpg

echo The echoed text
pause

https://i.stack.imgur.com/qu2IC.jpg

echo The echoed text
cmd /k

https://i.stack.imgur.com/TBLon.jpg

echo The echoed text & pause

https://i.stack.imgur.com/8p7hp.jpg


R
RAMM-HDR

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

r
robinCTS

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!


J
Julito Sanchis

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


Doesn't practically solve the question as modifying ipconfig and other built-in batch files is a very bad idea.
s
samadadi

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


R
RustyCake

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.


H
Huanfasdawq Huanfasd
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


s
sommmen

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.

https://i.stack.imgur.com/JhEBJ.png

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

https://i.stack.imgur.com/b2PY5.png

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.