Page 1 of 1

Is it possible to do "choice /t" without having a predetermined option?

Posted: 14 Apr 2017 19:29
by dubcusb
I don't know if I worded that correctly, but what I mean is:
Can I do something like

Code: Select all

@echo off
set "errorlevel=0"

choice /c yn /t 10
if "%errorlevel%"=="1" goto yes
if "%errorlevel%"=="2" goto no
goto maybe
I want to be able to select between two options and have them do something. If I don't select anything in 10 seconds, do something else.

I know I can simply add an extra option and set it as predetermined like

Code: Select all

choice /c ynm /t 10 /d m
but that way, I can directly press "M", instead of having to wait 10 seconds.

Is there anything that I can do to simulate what I want? Like an unchoosable option or something similar?

Thanks.


(Not native English speaker)

- dubcusb

Re: Is it possible to do "choice /t" without having a predetermined option?

Posted: 15 Apr 2017 04:23
by aGerman
There is no possibility using choice. I think the reason is that choice returns the position of the character in the string passed with option /c as errorlevel. What errorlevel should choice return if you didn't define the default? :wink:

Steffen

Re: Is it possible to do "choice /t" without having a predetermined option?

Posted: 15 Apr 2017 04:41
by Sounak@9434
A good way is to use a "not on keyboard" character. Like the alt+219 which is █ or alt+255 which is something like an invisible character (not tested). Then suppress the display of choices. Here is a small script.

Code: Select all

choice /c █yn /n /d █ /t 10 /m "[Y/N]"
If errorlevel 3 echo No
If errorlevel 2 echo Yes
If errorlevel 1 echo Who knows


Sincerely,
Sounak

Re: Is it possible to do "choice /t" without having a predetermined option?

Posted: 15 Apr 2017 10:28
by dubcusb
Sounak@9434 wrote:A good way is to use a "not on keyboard" character. Like the alt+219 which is █ or alt+255 which is something like an invisible character (not tested). Then suppress the display of choices. Here is a small script.

Code: Select all

choice /c █yn /n /d █ /t 10 /m "[Y/N]"
If errorlevel 3 echo No
If errorlevel 2 echo Yes
If errorlevel 1 echo Who knows


Sincerely,
Sounak


Thanks so much! That character isn't allowed on ANSI, but it gave me the idea of using another uncommon character instead (ý). Now it does exactly what I need. Thanks!

- dubcusb

Re: Is it possible to do "choice /t" without having a predetermined option?

Posted: 15 Apr 2017 11:40
by pieh-ejdsch
this will not use Errorlevel
and go to your choice in one way
or in unknown Language you want to use an IF ELSE

Code: Select all

echo off
for /f "tokens=1-2 delims=[,\/]" %%i in ('choice ^<nul 2^>nul') do (
  <nul set /p="[%%i,%%j]?"
  ( for /f "delims=Ô" %%A in ('choice /n /d Ô /c Ô%%i%%j /t 10') do (
      echo  %%A
       rem   if %%i == %%A ( * do this ) else * do that
      goto :answer%%A
    )
  ) || echo  -- time is over!
)
rem end
Exit /b
:answerJ
 rem * do this
echo this
Exit /b

:answerN
 rem * do that
echo that
Exit /b

Phil

Re: Is it possible to do "choice /t" without having a predetermined option?

Posted: 16 Apr 2017 03:40
by Sounak@9434
pieh-ejdsch wrote:this will not use Errorlevel
and go to your choice in one way
or in unknown Language you want to use an IF ELSE

Code: Select all

::Skipped

Phil


Mind for a little explanation on how the code works. Because I am :shock: :?:

Re: Is it possible to do "choice /t" without having a predetermined option?

Posted: 16 Apr 2017 07:32
by Aacini
pieh-ejdsch wrote:this will not use Errorlevel
and go to your choice in one way
or in unknown Language you want to use an IF ELSE

Code: Select all

snip

Phil


It is much simpler this way:

Code: Select all

choice /c yn■ /n /d ■ /t 10 /m "[Y/N]"
goto option-%errorlevel%


:option-1
echo Yes
. . .

:option-2
echo No
. . .

:option-3
echo Timeout
. . .

Don't forget to also define labels for option-0 (user press Ctrl-C) and option-255 (CHOICE error).

Antonio