[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3042
  • Last Modified:

Gracefully exit a looping batch file


What are some good methods for allowing users to gracefully exit a looping batch file?

=================
:loop
rem allow graceful exit here
goto :loop
=================


I've tried using CHOICE to allow users to hit a specific key to get out, but it appears that the choice command stagnates (hangs) if the command window of the batch file looses focus:
=================
:loop
echo hit 'Q' to exit this utility or 'Y' to manually refresh
choice /N /Ty,2 /Cqy
if %ERRORLEVEL%==1 goto :EOF
goto :loop
:EOF
exit
=================


I've also tried just telling user to hit Ctrl-C, but then they get the annoying "Terminate Batch Job Y/N" question:
=================
:loop
echo hit 'Ctrl-C' to exit this utility
sleep 3
goto :loop
=================


Any better ideas?

Thanks,
-Brian




0
brianadkins
Asked:
brianadkins
  • 2
  • 2
1 Solution
 
SteveGTRCommented:
You could put a time restraint on the choice command like so:

choice /CYN /TN,01 Press 'Y' to exit loop

if ERRORLEVEL 2 goto LOOP

This will give the user a 1 second window to abort during processing.

Other than that, I'd just have the user press CTRL-C and confirm with the Y/N prompt.

Good Luck,
Steve
0
 
InteractiveMindCommented:
There's always the pause statment:

echo Press any key to exit this program . . .
pause>nul
0
 
brianadkinsAuthor Commented:
Thanks for the ideas so far, but:

SteveGTR - that is essentially what I have above (just a one second wait instead of two)

InteractiveMind - Pause certainly is graceful, but it prevents the looping I'm after.

-Brian
0
 
SteveGTRCommented:
Sorry Brian didn't bother reading your batch file. I suppose that would help me to help you, but sometimes I get in too much of a hurry.

Here an assembler solution to your problem. Here's my test code:

@echo off

>temp.txt echo a
>>temp.txt echo mov ah,1
>>temp.txt echo int 16
>>temp.txt echo jz 10e
>>temp.txt echo mov ah,00
>>temp.txt echo int 16
>>temp.txt echo mov al,1
>>temp.txt echo jmp 110
>>temp.txt echo mov al,0
>>temp.txt echo mov ah,4c
>>temp.txt echo int 21
>>temp.txt echo.
>>temp.txt echo r cx
>>temp.txt echo 14
>>temp.txt echo w
>>temp.txt echo q

type temp.txt|debug _temp.com 2>NUL >NUL

:LOOP

echo Press any key to exit...

_temp.com
if ERRORLEVEL 1 goto STOP

goto :LOOP

:STOP

del temp.txt>NUL
del _temp.com>NUL

echo Done!

Good Luck,
Steve
0
 
brianadkinsAuthor Commented:
Thanks for the help...  I have no understanding of assembly code, but your method was functional.  Turns out I almost had a seizure from the flashing command windows opening and closing... : )

I'll probably just end up going with option A which was Press Ctrl-C to exit ... and deal with the "Terminate batch Job" question as needed.

Thanks for the suggestions guys...

-Brian
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now