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?
17 Answers
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
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
Possibility 1: Just make 2 .bat files and write into the first:
start <filename> // name of 2nd batch file
exit
Batch file 2 is the file that wont close in the end. So now when you open batch nr.1 It will start the 2nd and cloe itself. When the 2nd finishes it will not close entirely (as long as you wont put exit at the end)
Possibility 2: Batch file 1:
call <filename>
cls
echo End of file
pause
<any code you want>
When the 2nd file ends then it will proceed to file 1 again and output the rest of it. With that you can even make error handlers. If nr.1 crashes it goes into nr.2 and displays it
Easy, add cmd to your last line of bat, BUT! if you reset or clear your system path, you must start your cmd with the full path, like:
%windir%\system32\cmd.exe
For example, I have a bat file to reset jdk to old version like this:
PATH=C:\Program Files\Java\jdk1.6.0_45\bin;C:\apache-ant-1.7.1\bin
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45
%windir%\system32\cmd.exe
since I reset the system path, I have to run cmd with the full path, or the system can't find cmd.exe, it will fail to run cmd, and just close the window, and you can't see the error msg.