Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
"I'm developing a complex, long-running batch script" -- well, there's your first problem right there... –  Aaron Miller Aug 26 '13 at 17:01
1  
Please don't comment on decisions already made regarding implementation, as that is not the point of this question. –  mellamokb Aug 26 '13 at 17:03
    
What version of Windows? In my hands, The Ctrl-C works as you want on Windows 7. –  dbenham Aug 26 '13 at 17:25
    
@dbenham: This is Windows 7, 64-bit. Hmm, it doesn't work for me. Can you upload the script you are testing with? –  mellamokb Aug 26 '13 at 17:46

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
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 Aug 26 '13 at 18:31
    
@mellamokb - Are you redirecting input at any point? –  dbenham Aug 26 '13 at 18:34
    
Nope. But I am using a lot of nested if/for clauses, something like for /l %%y in (2012, 1, 2013) do ( ... pause ... ) –  mellamokb Aug 26 '13 at 18:38
    
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: pastebin.com/BEtSKBer –  mellamokb Aug 26 '13 at 18:40
    
@mellamokb - I appended a solution that should work within blocks of code. It was an interesting problem to solve. –  dbenham Aug 26 '13 at 21:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.