A Windows XP help forum. PCbanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » PCbanter forum » Microsoft Windows 7 » Windows 7 Forum
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

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



 
 
Thread Tools Rate Thread Display Modes
  #1  
Old August 11th 18, 01:47 PM posted to alt.windows7.general
No_Name
external usenet poster
 
Posts: 21
Default 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?

  #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.
 




Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off






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


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