67

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?

Samaursa
  • 16,527
  • 21
  • 89
  • 160

8 Answers8

164

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
Tim
  • 7,660
  • 3
  • 30
  • 33
  • 18
    Thanks for this, worked for me too. On a side note, seems that it doesn't have to fail, if the command before has any type of exit code (even on success) in it then it seems to close the window. – dann.dev Apr 25 '12 at 22:35
  • 1
    Adding `call` had no effect for me on Windows 10 command prompt running as admin. I don't know why the `pause` command in my batch files (opened by AutoHotKey) suddenly stopped working today. I don't know what changed. They'd worked for weeks. – Ryan Jan 17 '17 at 15:35
  • 3
    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 – Justin Jul 19 '17 at 01:36
  • To give a bit more explanation, in CMD (and in MS-DOS before it) running a batch file ends execution of the current batch file. This means that, in the original example, nothing after the `phpunit` line will be run (i.e. it's nothing to do with `pause`, per se). If you want execution of the current batch file to continue after running the external batch file, you need to use the `call` command, as per this answer. – HappyDog Dec 04 '20 at 13:38
3

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.

Vivek Madani
  • 287
  • 2
  • 11
  • Well, if `pause` *is* called, it will work. Using `timeout` instead will not change that (except that it's calling a separate program instead of a shell-builtin). My guess would be that there is a `goto :eof` or `exit /b` somewhere before the `pause` (not uncommon in batch files with subroutines – where you cannot just add commands to the end to get them executed). – Joey Jan 13 '11 at 16:32
  • What do you do if last command throws an exception, which is my case? – Juan José Melero Gómez Apr 21 '16 at 08:45
2

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
Zunair
  • 1,085
  • 1
  • 13
  • 21
0

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

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
Justin
  • 927
  • 11
  • 24
0

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

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

Jamesfo
  • 524
  • 6
  • 11
  • Sounds logical enough, but isn't correct. `start /b` starts a new instance in another thread. There is no possiblity for the main thread to *see* the output of that thread. – jeb Nov 18 '19 at 05:44
0

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.

Dominique
  • 16,450
  • 15
  • 56
  • 112
MtheK
  • 1