Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test for button press on pause command in batch?

Tags:

batch-file

I am writing a batch script, in which a user reads a disclaimer, then they press any key to continue, or "E" to exit. It looks something like this:

@echo off
echo (some disclaimer text here)
echo.
echo once you fully understand this message, press any key to continue, or press "E" to exit.
::I know that pause doesn't take input, I'm just using it as a placeholder for something
pause>nul
cls
echo Welcome!
pause

Is there any way to accomplish this? Thanks!

To clarify, I want to exit the second they press the E key. I have seen it done before, but I forgot where it saw it.

like image 836
Shane Smiskol Avatar asked Jun 17 '26 08:06

Shane Smiskol


1 Answers

Choice.exe does what you describe, though it doesn't have an option for "any key". You can specify a specific key such as "Y" below.

echo once you fully understand this message, press "Y" to continue, or press "E" to exit.
choice /c ey
if errorlevel 2 goto :welcome
if errorlevel 1 goto :canceled

rem User pressed Ctrl+C

:canceled
rem User pressed E.  Do cancel stuff.
goto :eof

:welcome
rem User pressed Y.  Do welcome stuff
echo Welcome!
like image 150
Ryan Bemrose Avatar answered Jun 21 '26 15:06

Ryan Bemrose



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!