Alternative to CHOICE - need help
Posted: 02 Nov 2015 12:34
I am experimenting with an alternative to CHOICE and have taken code from a post
on this forum that uses xcopy to capture a keystroke. I've tried it with echo on and
it looks like it should work, comparing entries with valid choices, but results are not
consistent.
Choices are 1, 2, 3 or q.
If I enter 1, it tests to be invalid. If I enter 1 again, it is a valid choice.
If I enter 4 (not a valid choice) it tests to be a valid choice, and if I enter 4 again,
it tests to be invalid. If I enter q, it tests not valid but proceeds to quit anyway.
This batch code is just to explore how keystrokes can be trapped, and I hope with your
help, will evolve into a bug-free function.
Thanks for your advice and any other tips about this CHOICE alternative.
on this forum that uses xcopy to capture a keystroke. I've tried it with echo on and
it looks like it should work, comparing entries with valid choices, but results are not
consistent.
Choices are 1, 2, 3 or q.
If I enter 1, it tests to be invalid. If I enter 1 again, it is a valid choice.
If I enter 4 (not a valid choice) it tests to be a valid choice, and if I enter 4 again,
it tests to be invalid. If I enter q, it tests not valid but proceeds to quit anyway.
This batch code is just to explore how keystrokes can be trapped, and I hope with your
help, will evolve into a bug-free function.
Thanks for your advice and any other tips about this CHOICE alternative.
Code: Select all
@Echo Off
rem simulate the CHOICE function
setlocal
Set "choices=1, 2, 3, q"
:top
Set KEY=
Echo(&Echo Enter your choice: %choices%
For /f "delims=" %%a In ('Xcopy /l /W "%~f0" "%~f0" 2^>nul') Do (
If Not defined KEY Set "KEY=%%a"
)
Set KEY=%KEY:~-1%
If "%KEY%"==" " (
Echo You pressed the space bar.
) Else If defined KEY (
Set "validEntry=FALSE"
For %%a In (%choices%) Do If /I "%%a"=="%KEY%" Set "validEntry=TRUE"
If "%validEntry%"=="TRUE" (Echo Entry is valid: %KEY%) Else Echo Entry not valid: %KEY%
) Else (
Echo How did this happen?
)
If /I "%KEY%"=="q" GoTo:eof
GoTo:top
endlocal