Without press <ENTER>

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Without press <ENTER>

Post by MigrationUser »

19 May 2007 18:24
Fen_Li

Code: Select all

@echo off
:up
set /p ans=your answer : 
if /i "ans" equ "y" goto yes
goto up

:yes 
echo your answer is yes
pause>nul
My StupidQ: if I want to see "your answer is yes" in console I must type y then <ENTER>
how if not have to press <ENTER> ?
so, when I type "y" (without press <ENTER>) appear message "your answer is yes"

Last edited by Fen_Li (19 May 2007 19:32)

G-Smart thing Smart

----------------------------

#2 21 May 2007 12:24
NDog


This is how I do it. Maybe someone else will have better suggestions though.

Code: Select all

@echo off
:up
ECHO Choose an answer:
ECHO.
ECHO   (Y)es
ECHO   (N)o
ECHO   e(X)it
ECHO.

CHOICE /N /C:YNX "your answer : "

IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 SET FUNCTION=yes&GOTO START
IF ERRORLEVEL 2 IF NOT ERRORLEVEL 3 SET FUNCTION=up&GOTO START
IF ERRORLEVEL 3 IF NOT ERRORLEVEL 4 SET FUNCTION=exit&GOTO START

:START
goto %function%
goto exit

:yes 
echo your answer is yes
pause>nul

:exit
exit
cmd, vbs, ps, bash
autoit, python, swift

----------------------------

#3 25 May 2007 15:06
bluesxman


No better suggestions, but you are over complicating things a bit there, NDog. One could simplify it thus:
** UNTESTED **

Code: Select all

@echo off
:up
ECHO Choose an answer:
ECHO.
ECHO   (Y)es
ECHO   (N)o
ECHO   e(X)it
ECHO.

CHOICE /N /C:YNX "your answer : "

IF ERRORLEVEL 1 SET FUNCTION=yes
IF ERRORLEVEL 2 SET FUNCTION=up
IF ERRORLEVEL 3 SET FUNCTION=exit

goto %function%

:yes 
echo your answer is yes
pause>nul

:exit
exit
Or, simpler still...

Code: Select all

@echo off
:up
ECHO Choose an answer:
ECHO.
ECHO   (Y)es
ECHO   (N)o
ECHO   e(X)it
ECHO.

CHOICE /N /C:YNX "your answer : "

IF ERRORLEVEL 3 exit
IF ERRORLEVEL 2 goto up

echo your answer is yes
pause>nul
cmd | *sh | ruby | chef

----------------------------

#4 31 May 2007 17:38
Fen_Li


thanks all..;) smile
nice reply, maybe more simple like this :

@echo off
ECHO Choose an answer:
ECHO.
ECHO (Y)es
ECHO (N)o
ECHO e(X)it
ECHO.

CHOICE /N /C:YNX "your answer : "

IF ERRORLEVEL 1 echo your answer is yes
IF ERRORLEVEL 2 echo your answer is no
IF ERRORLEVEL 3 exit
pause>nul

I'am sorry if i wrong...
regard from indonesia..

G-Smart thing Smart

----------------------------

#5 01 Jun 2007 11:39
bluesxman


No, because of the way "IF ERRORLEVEL" works.

Testing for ERRORLEVEL is effectively a greater than or equal to -- IE testing for a given number matches that AND any higher numbers. So your script would work fine if you pressed "Y" and it would say "your answer is yes" and pause. But if you were to input "N" your script would say "your answer is yes" then "your answer is no" and pause. Similarly if you were to input "X" your script would say "your answer is yes" then "your answer is no" and then instantly exit.

This functionality is why NDog used statements like "IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2".

Last edited by bluesxman (02 Jun 2007 02:57)

cmd | *sh | ruby | chef

----------------------------

#6 03 Jun 2007 17:39
Fen_Li


upss...sorry..
I not test that script..
thanx for correct my posting...
gbu.. smile

G-Smart thing Smart
Post Reply