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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dubcusb
Posts: 3
Joined: 13 Oct 2016 16:21

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

#1 Post by dubcusb » 14 Apr 2017 19:29

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 15 Apr 2017 04:23

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

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

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

#3 Post by Sounak@9434 » 15 Apr 2017 04:41

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

dubcusb
Posts: 3
Joined: 13 Oct 2016 16:21

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

#4 Post by dubcusb » 15 Apr 2017 10:28

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

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

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

#5 Post by pieh-ejdsch » 15 Apr 2017 11:40

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

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

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

#6 Post by Sounak@9434 » 16 Apr 2017 03:40

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: :?:

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#7 Post by Aacini » 16 Apr 2017 07:32

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

Post Reply