How to prevent auto-closing of console after the execution of batch file
Solution 1
In Windows/DOS batch files:
pause
This prints a nice "Press any key to continue . . . "
message
Or, if you don't want the "Press any key to continue . . ."
message, do this instead:
pause >nul
Solution 2
Depends on the exact question!
Normally pause
does the job within a .bat file.
If you want cmd.exe not to close to be able to remain typing, use cmd /k
command at the end of the file.
Solution 3
If you want cmd.exe to not close, and able to continue to type, use
cmd /k
Just felt the need to clarify what /k
does (from windows website):
/k
: Carries out the command specified by string and continues.
So cmd /k
without follow up command at the end of bat file will just keep cmd.exe
window open for further use.
On the other hand pause
at the end of a batch file will simply pause the process and terminate cmd.exe
on first button press
Solution 4
If you are using Maven and you want to skip the typing and prevent the console from close to see the result you need to use the CALL
command in the script, besides just the 'mvn clean install'.
Like this will close the console
ECHO This is the wrong exemple
mvn clean install
pause
Like this the console will stay open
ECHO This is the right exemple
CALL mvn clean install
pause
If you dont use the CALL
command neither of the pasts exemples will work. Because for some reason the default behaviour of cmd when calling another batch file (which mvn is in this case) is to essentially replace the current process with it, unlike calling an .exe
Solution 5
The below way of having commands in a batch file will open new command prompt windows and the new windows will not exit automatically.
start "title" call abcd.exe param1 param2
start "title" call xyz.exe param1 param2

VichitraVij
hi, I live in conshohocken, work in malvern. My App: http://itunes.apple.com/us/app/kickoff/id427696915?mt=8 My Website: http://nofouls.com My Band: http://www.facebook.com/TheColinizers
Updated on January 25, 2022Comments
-
VichitraVij over 1 year
What command can I put at the end of a batch file to prevent auto-closing of the console after the execution of the file?