View Single Post
  #2  
Old August 11th 18, 08:30 PM posted to alt.windows7.general
VanguardLH[_2_]
external usenet poster
 
Posts: 10,881
Default How do you make a batch file accept 1-char keyboard input WITHOUT having to also press carriage return?

jackpatton wrote:

Bob J Jones wrote:

How do you make a batch file accept single-character keyboard input
WITHOUT having to also press carriage return?


I see this and wonder how many people remember running DOS and
ANSI.SYS and ending a batch command with 13p(If i recall correctly).
Who remembers?


Use the 'choice' command. Default is to capture either Y or N keys but
you can add other choices. No having to press the Enter key after
hitting a key for a choice. Run "choice /?" to see its help. The
choice sets the errorlevel value, so you have to know how errorlevel
gets compounded when testing on its value if the "if" command.

If the errorlevel value is 2, testing on 0, 1, or 2 returns true. View
"if errorlevel n" as "if errorlevel = n". Conversely, when using the
negation, view "if not errorlevel n" as "if errorlevel n". This can
get confusing, so it better to use the following operators:

EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal

With those, you have to use the ERRORLEVEL environment variable's value,
not the environment variable's name. "if errorlevel ..." is using the
name of the environment variable: errorlevel. "if %errorlevel% ..."
tests on the value of the substitued environment variable's value."

To test on, say, a value of only 2 (not 2 or more) for the errorlevel
environment variable's current value, use "if %errorlevel% equ 2 ...".
See https://ss64.com/nt/if.html for more info.

If you don't want to effect an current errorlevel value, use setlocal.
Envvars defined after a setlocal have a scope only within that setlocal
shell (until an endlocal is encountered or when the batch script exits).
Run "setlocal /?" for more help.

Between the choice and if commands, the OP should get what he asked for.
Ads