61
votes

I am creating a simple batch file to assist in a few things, and I have some instructions that it prints out as well that I want the user to see before exit. Currently, the window closes very quickly. So I added PAUSE at the end of the file, but it does not want to work.

I looked at other questions on SO and have checked to make sure the line endings are CRLF and that I have CRLF at the end of the file.

Any suggestions?

7
You're doing something else wrong. I just tried it again and it works perfectly fine for me.Cody Gray♦
I honestly do not know why, but it has started working today. My apologies for this, but I have no idea what happened. I did not even change the script!Samaursa

7 Answers

146
votes

If the last command fails pause won't work.

You can fix it by putting "call" behind the command you are running (whatever command is before the pause) then the pause will work.

So for example I had a phpunit batch file that looked like this:

phpunit tests/sometests.php
pause

When phpunit failed it just exited without pausing. Changing it to this made it pause correctly:

call phpunit tests/sometests.php
pause
3
votes

Does the last command before pause execute successfully? Mind sharing your script - at least last few commands?

Alternatively, since you seem to be using Windows7, try Timeout command and see if that is working.

2
votes

I was having issues even on echo... assuming it was caused by long batch file... Pause was executing but it was not pausing, it was almost like that it was pressing a key after Pause was executed.

Tried suggested solutions above; none worked.

So just for future reference, here is what I did:

Basically just "pause > nul && pause > nul"; works every time.

@echo off

CALL :ForcePause "Press any key to resume."
ECHO.
ECHO Hello World!
ECHO.
CALL :ForcePause "Press any key to exit."

EXIT

REM You can remove echo if you don't want to pass custom string for pause
:ForcePause
echo %~1
pause > nul && pause > nul
GOTO :EOF
0
votes

I think i know where is the issue, i had the same problem. So if you are making it like this, you creating a new file and put all the batch info inside it and save it like normal text and after this just rename the extension it won't work :). You have to save it via the text editor and from there you have to choose "Batch file(.bat;.cmd;*.nt)" for example Notepad++, its probably because of the encoding so do it like this and i thnk it will be ok. GL ! :)

0
votes

Just addition to Tim's answer, if you want the window to always display, you can write the .bat file like:

call phpunit tests/sometests.php
cmd /k
0
votes

I have the same problem. This occurs because of two reasons:
The first one is when running the batch file as administrator.

I have moved my batch file to desktop and try it as administrator and it works fine.
I have tried moving the batch file to any disk root (like C:\ D:\ etc.) and it works correctly.
I have tried moving the batch file to any directory with spaces and it works correctly.

The second and main reason is: There was some special characters (like @ or & or something like that) in the batch file's directory.

Just find this special character and remove it from the folder name. And it will work correctly.

0
votes

I couldn't understand why pause was only briefly pausing in my script. The answers here helped me solve the problem, and I finally figured out what was happening in my case:

start /b

If you run an application with start /b it is possible the application will send some output to stdout after a delay. If that output comes when the pause command is running, it will be interpreted as keystrokes and, naturally, the script will continue. You will most certainly have this issue if you use start /b with commands that connect to remote computers:

start /b plink [email protected] -pw raspberry "sudo apt-get update"
pause

The workaround is to use pause >nul && pause instead