How to prevent auto-closing of console after the execution of batch file

Viewed 862922

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?

21 Answers

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

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.

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

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

Had problems with the answers here, so I came up with this, which works for me (TM):

cmd /c node_modules\.bin\tsc
cmd /c node rollup_build.js
pause

This little hack asks the user to enter a key and stores it into the variable %exitkey% (this variable could be called anything you like though).

set /p exitkey= "Press any key to continue..."

NB: the space after the '=' is very important

pause

or

echo text to display
pause>nul

Run the .exe file and then pause the cmd

batch script example :

@echo off
myProgram.exe
PAUSE

batch script example with arguments :

@echo off
myProgram.exe argumentExample1 argumentExample2
PAUSE

I added @echo off because I don't want to show C:\user\Desktop>myProgram.exe and C:\user\Desktop>PAUSE in the cmd

cmd /k cd C:\Projects.....

If you want your cmd opened at specific long location

add pause (if you don't want anything else to show up add) >nul
it should look like:

@echo off
title nice
echo hello
pause >nul

all you will see is "hello"

I personally put pause >nul and it waits for a key to be pressed without showing any extra text in the console.

using : call yourbatch.cmd

does the job will process the script and then continue ejecuting other code on same window (cmd instance)

Related