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?
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 likefor /l %%y in (2012, 1, 2013) do ( ... pause ... )
– mellamokb – 2013-08-26T18:38:14.857I think I figured it out. If there are multiple
– mellamokb – 2013-08-26T18:40:00.707pause
statements inside of a singlefor
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 forpause
statements: http://pastebin.com/BEtSKBer@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