Break Batch Script Execution during Pause

3

I'm developing a complex, long-running batch script, and came upon a little issue when debugging. I have a command-line window open to the root path of the script, so I can type myscript.bat to fire it off for testing. I am using PAUSE commands between statements to watch the behavior of the script. However, I discovered that typing CTRL+C during a PAUSE command to cancel execution is simply interpreted as a key to continue execution, rather than breaking the script as intended.

I know I can simply kill/close the command window, but it's a little annoying to navigate back to the correct path in a new command-window every time. Is there a way to properly break a batch script's execution easily while it is currently in a PAUSE?


Edit: It looks like PAUSE works fine in simple scripts, as indicated by @dbenham. However, if you have multiple PAUSE statements nested inside of a FOR iteration, then you can't break in the middle of the FOR iteration - only at the end. Here is a sample script that demonstrates the issue:

@echo off

for /l %%y in (2008, 1, 2013) do (
    echo 1
    pause
    echo 2
    pause
    echo 3
    pause
    echo 4
    pause
    echo 5
    pause
)

If you try to terminate any of the first four PAUSE statements, you will find that your terminate command is ignored and execution continues anyway. But once you reach the end, to the fifth PAUSE, you do get the terminate prompt (in the example output below, I was pressing CTRL+C at every prompt):

Z:\>test
1
Press any key to continue . . .
2
Press any key to continue . . .
3
Press any key to continue . . .
4
Press any key to continue . . .
5
Press any key to continue . . .
Terminate batch job (Y/N)? y

Any ideas how to prevent this behavior?

mellamokb

Posted 2013-08-26T16:53:27.920

Reputation: 215

Answers

3

It works fine for me on Win 7 (32 bit)

Here is a trivial test script

@echo off
echo Before
pause
echo After

When I run the above without pressing <Ctrl-C>, I get the following:

D:\test>test
Before
Press any key to continue . . .
After

D:\test>

When I press <Ctrl-C> at the "Press any key" prompt, I get:

D:\test>test
Before
Press any key to continue . . .
Terminate batch job (Y/N)? y

D:\test>

EDIT: Solution to follow-up question

It doesn't need to be in a loop. The problem exists with any parenthesized block of code. The entire block is parsed in one pass and then executed from memory.

(
  echo Before 1
  pause
  echo Before 2
  pause
  echo After
)

I don't think there is a fix using the PAUSE command. But I have a simple solution that should give the debug functionality you want. Just make sure that the PAUSE environment variable is not already defined before you run your script.

@echo off

:: ------------------------------------------------------
:: Include this block of code at the top of your script
::
setlocal
if not defined pause (
  set "pause=choice /m "........................Continue "&if errorlevel 2 exit"
  cmd /c ^""%~f0" %* ^"
  exit /b
)
:: ------------------------------------------------------


:: Now use %pause% instead of pause anywhere you want within your script
:: Press Y to continue, N to quit
:: The script will immediately terminate if you press N, even if in the
:: middle of a CALL

:: Here is a simple demonstration

call :sub1
exit /b

:sub1
call :sub2
exit /b

:sub2
for /l %%N in (1 1 3) do (
  echo ----- %%N ------
  echo Before 1
  %pause%
  echo Before 2
  %pause%
  echo After
)
exit /b

dbenham

Posted 2013-08-26T16:53:27.920

Reputation: 9 212

Hmm, I do too. There must be something unique about my script. Let me see if I can make a script that reproduces the problem. – mellamokb – 2013-08-26T18:31:28.953

@mellamokb - Are you redirecting input at any point? – dbenham – 2013-08-26T18:34:40.713

Nope. But I am using a lot of nested if/for clauses, something like for /l %%y in (2012, 1, 2013) do ( ... pause ... ) – mellamokb – 2013-08-26T18:38:14.857

I think I figured it out. If there are multiple pause statements inside of a single for iteration, then you can't actually break the script until the end of that iteration. Weird. Try this script for example, and you can't break the first for pause statements: http://pastebin.com/BEtSKBer

– mellamokb – 2013-08-26T18:40:00.707

@mellamokb - I appended a solution that should work within blocks of code. It was an interesting problem to solve. – dbenham – 2013-08-26T21:28:31.690

That is an awesome idea! Thanks for taking the time to look at it. This should work great. – mellamokb – 2013-08-26T21:58:22.787

1

One way to overcome the problem of PAUSE in parenthesized blocks is to call a subroutine:

@echo off
goto run

:pause
echo %*
pause >nul
exit /b

:run
(
  echo Before 1
  call :pause Please consider aborting with Ctrl+C
  echo Before 2
  call :pause Really consider aborting with Ctrl+C
  echo After
)

This solves the problem at least in Windows 10, and allows custom messages for PAUSE.

stevek_mcc

Posted 2013-08-26T16:53:27.920

Reputation: 640