PCbanter

PCbanter (http://www.pcbanter.net/index.php)
-   Windows 7 Forum (http://www.pcbanter.net/forumdisplay.php?f=48)
-   -   How do you make a batch file accept 1-char keyboard input WITHOUT having to also press carriage return? (http://www.pcbanter.net/showthread.php?t=1105437)

[email protected] August 11th 18 01:47 PM

How do you make a batch file accept 1-char keyboard input WITHOUT having to also press carriage return?
 
On Tue, 29 May 2018 00:13:59 +0000 (UTC), 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?


VanguardLH[_2_] August 11th 18 08:30 PM

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.


All times are GMT +1. The time now is 10:53 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright © 2004 - 2006 PCbanter
Comments are property of their posters