Batch file command PAUSE does not work

Windows 7Batch File

Windows 7 Problem Overview


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?

Windows 7 Solutions


Solution 1 - Windows 7

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

Solution 2 - Windows 7

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.

Solution 3 - Windows 7

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

Solution 4 - Windows 7

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 ! :)

Solution 5 - Windows 7

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

Solution 6 - Windows 7

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.

Solution 7 - Windows 7

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 pi@192.168.1.100 -pw raspberry "sudo apt-get update"
pause

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

Solution 8 - Windows 7

FYI: nothing shown above worked for me in Win7.
However, this DID finally work:

PAUSE 0<CON

This also did NOT work for 'timeout'.
Without it, it gets this:

ERROR: input redirection not supported,...

With it, it gets this:

ERROR: The handle is invalid.

In my case, this occurred after doing this:

start /WAIT cmd.exe /V:ON /k "set IEdir & pushd %IEdir% & cd & call "C:\Program Files\Internet Explorer\iexplore.exe" & set XsetRC1=!errorlevel! & set Xset & echo Xrc1=!XsetRC1! & popd & timeout /T 3 & EXIT !XsetRC1!"

where IE returns an RC=1 (what a KILL also gets).

However, doing the exact same for FireFox and Thunderbird:

start /WAIT cmd.exe /V:ON /k "set FFdir & pushd %FFdir% & cd & call "C:\FireFox\FireFox.exe" & set XsetRC1=!errorlevel! & set Xset & echo Xrc1=!XsetRC1! & popd & timeout /T 3 & EXIT !XsetRC1!"

where FF and TB returns an RC=0, 'timeout' DOES work properly.

Also note that PAUSE does NOT fail in Win10.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamaursaView Question on Stackoverflow
Solution 1 - Windows 7TimView Answer on Stackoverflow
Solution 2 - Windows 7Vivek MadaniView Answer on Stackoverflow
Solution 3 - Windows 7ZunairView Answer on Stackoverflow
Solution 4 - Windows 7Atanas KovachevView Answer on Stackoverflow
Solution 5 - Windows 7JustinView Answer on Stackoverflow
Solution 6 - Windows 7Mohammad Huda KnefatyView Answer on Stackoverflow
Solution 7 - Windows 7JamesfoView Answer on Stackoverflow
Solution 8 - Windows 7MtheKView Answer on Stackoverflow