How to display the choice /t timer?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

How to display the choice /t timer?

#1 Post by PaperTronics » 10 Jul 2017 05:47

Hey guys!

I'm using the /t switch of the choice command for the first time, and I thought that it will display the time in which in the default choice will be selected,much like the timeout command, but I was astonished as it didn't.

So is there a way to make the choice command display the timer?


Thanks,
PaperTronics

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to display the choice /t timer?

#2 Post by miskox » 10 Jul 2017 06:59

Maybe something like this:

Code: Select all

@echo off
set /a cnt=30
:0
cls
REM title %cnt%
if not "%cnt%"=="0" choice /C YNT /N /T 1 /D T /M "Please choose Y or N in %cnt% sec.: "
set errlev=%errorlevel%
if %errlev%==3 set /a cnt-=1&goto :0
echo Selected:%errlev%


Of course some more code (error checking..., default value...) should be added.

Saso

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to display the choice /t timer?

#3 Post by PaperTronics » 11 Jul 2017 03:03

@miskox - I appreciate the effort, I had thought of the same method too before posting the question, but because of screen flicker I rejected it. If there's any way (or plugin) to get around the screen flicker then I'm interested :)

Thanks,

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to display the choice /t timer?

#4 Post by miskox » 11 Jul 2017 04:52

PaperTronics wrote:@miskox - I appreciate the effort, I had thought of the same method too before posting the question, but because of screen flicker I rejected it. If there's any way (or plugin) to get around the screen flicker then I'm interested :)

Thanks,



Of course there are methods to move cursor without flickering. (viewtopic.php?t=7898)

Saso

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to display the choice /t timer?

#5 Post by PaperTronics » 11 Jul 2017 05:53

miskox wrote:Of course there are methods to move cursor without flickering.


Oh! You don't get it, I meant that if there are methods to do the same thing without cls' screen flicker. I hide the mouse cursor using plugins so that's taken care of. The screen flicker is what makes the method unreliable.


Thanks,
PaperTronics

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: How to display the choice /t timer?

#6 Post by misol101 » 11 Jul 2017 10:30

I'm sure other tools could accomplish the same, but anyway, using gotoxy.exe (and borrowing miskox script) :

Code: Select all

@echo off
set /a cnt=15
:0
   if not "%cnt%"=="0" choice /C YNT /N /T 1 /D T /M "Please choose Y or N in %cnt% sec.: "
   if "%cnt%"=="0" echo(
   set errlev=%errorlevel%
if %errlev%==3 gotoxy k /1 & set /a cnt-=1 & goto :0
echo Selected:%errlev%

The problem with this code is that the default selection (T) is printed by choice on timeout. The best thing would be if choice could select it without printing it or if we could have an invisible character as an option for choice, but I don't know if that is possible.

Trying to erase the "T". This works, but you can still see the unwanted T flicker by sometimes, before gotoxy erases it.

Code: Select all

@echo off
set /a cnt=15
:0
   set STR="Please choose Y or N in %cnt% sec.:      "
   if "%cnt%"=="0" echo(
   if not "%cnt%"=="0" choice /C YNT /N /T 1 /D T /M %STR%
   set errlev=%errorlevel%
if %errlev%==3 gotoxy k /1 %STR% & set /a cnt-=1 & goto :0
echo Selected:%errlev%


Edit: looking at Aacini's example, T can be replaced by ò , ( showing as = ), which at least looks better than a mysterious T :)

Edit2: aah, I see that Aacini has updated with a BS approach too to erase the char. That would work here too I guess
Last edited by misol101 on 11 Jul 2017 11:08, edited 3 times in total.

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

Re: How to display the choice /t timer?

#7 Post by Aacini » 11 Jul 2017 10:37

This pure Batch method works with no flickering because it does not use CLS, but the cursor move method suggested by @miskox:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Define variables for cursor positioning
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"

rem Assume a window width of 80 characters
set "BSs="
for /L %%i in (1,1,12) do set "BSs=!BSs!!BS!"

set /a cnt=30
:0
REM title %cnt%
if not "%cnt%"=="0" choice /C YNò /N /T 1 /D ò /M "Please choose Y or N in %cnt% sec.:   !BS!!BS!!BS! !BS!"
if errorlevel 3 set /a cnt-=1&echo %TAB%!BSs!&goto :0
echo Selected: %errorlevel%

Antonio

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to display the choice /t timer?

#8 Post by PaperTronics » 11 Jul 2017 21:39

@Aacini - The script that you provided doesn't work on my computer, it just shows the error:

Code: Select all

ERROR: Invalid syntax. /D only accepts single character.
Type "CHOICE /?" for usage.


I haven't modified the script, I just copy/pasted it to the batch file but it refuses to work as desired.


@misol101 - I'm quite frustrated too by the mysterious T showing up, that's why I didn't test your script. Because the T just ruins the whole thing for me. :cry:




PaperTronics

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: How to display the choice /t timer?

#9 Post by misol101 » 12 Jul 2017 01:35

PaperTronics wrote:@misol101 - I'm quite frustrated too by the mysterious T showing up, that's why I didn't test your script. Because the T just ruins the whole thing for me. :cry:

Like I suggested in the edit, replace T by ò (shows as = )

Code: Select all

@echo off
set /a cnt=15
:0
   set STR="Please choose Y or N in %cnt% sec.:      "
   if "%cnt%"=="0" echo(
   if not "%cnt%"=="0" choice /C YNò /N /T 1 /D ò /M %STR%
   set errlev=%errorlevel%
if %errlev%==3 gotoxy k /1 %STR% & set /a cnt-=1 & goto :0
echo Selected:%errlev%

About the error you got with Aacini's script: you will most likely get the same error with the script above. For ò to work properly, you need to have the document type set as Ansi/Ascii when pasting this, not Utf. For example Notepad++ allows you to set this.

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to display the choice /t timer?

#10 Post by PaperTronics » 12 Jul 2017 04:30

@misol101 -
misol101 wrote:About the error you got with Aacini's script: you will most likely get the same error with the script above. For ò to work properly, you need to have the document type set as Ansi/Ascii when pasting this, not Utf. For example Notepad++ allows you to set this.

Although I use Sublime Text for programming, I do have Notepad++ installed on my computer for situations like these. I converted Aacini's script to Ansi and after converting it works properly but after it reaches the 29th second it shows this error:

Code: Select all

PThe system cannot write to the specified device.
Please choose Y or N in 29 sec.:


Now, I just tested your script and even your script doesn't work as desired. It has a strange output:

Code: Select all

Please choose Y or N in 14 sec.:   !BS!!BS!!BS! !BS! ≥
!BSs!
Please choose Y or N in 12 sec.:   !BS!!BS!!BS! !BS! ≥
!BSs!
Please choose Y or N in 10 sec.:   !BS!!BS!!BS! !BS!


Need some help here :cry:





Thanks,
PaperTronics

misol101
Posts: 475
Joined: 02 May 2016 18:20

Re: How to display the choice /t timer?

#11 Post by misol101 » 12 Jul 2017 06:32

There is absolutely no !BS! in any of the scripts I posted, which means you have modified my scripts, which means I don't know what they are doing any more.

Just run the last script I posted, without modifications (and convert it to Ansi)

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

Re: How to display the choice /t timer?

#12 Post by Aacini » 12 Jul 2017 06:47

PaperTronics wrote:I converted Aacini's script to Ansi and after converting it works properly but after it reaches the 29th second it shows this error:

Code: Select all

PThe system cannot write to the specified device.
Please choose Y or N in 29 sec.:


PaperTronics


Well, you forget to say that this error just happen when the CHOICE prompt is displayed in the first screen line (when a CLS command is executed before), but after that the program continue correctly with the prompt in the second line... :cry:

This is a limitation of the method used to move the cursor; for further details, see the linked topic.

However, if you want that the CHOICE prompt appear in the first line, you may use the original CLS method; the flicker will be very small...

Antonio

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to display the choice /t timer?

#13 Post by PaperTronics » 12 Jul 2017 23:18

@misol101 - Oops! Sorry, all the scripts posted in this topic were mixed up in one batch file, and since all of them had 1 common label :0, so the goto command wasn't jumping back to start, instead it was to jumping Aacini's script which contained the !BS! variable causing the error I described above.

I just tested your script again, in a new batch file and it works perfectly! Although sometimes this strange character shows up:
>. I'm fine with that though as there is no error of any kind in the script and it works smoothly. Thanks for your help bro.

@Aacini - Since misol101's script is working fine, I'm gonna stick to it. If there's any other method or plugin to get around this limitation then I'm interested.




PaperTronics

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: How to display the choice /t timer?

#14 Post by PaperTronics » 19 Jul 2017 02:58

Another problem has surfaced. I was using misol101's script all these days, and it worked just fine, until I added 3 choices to choose from instead of 2.

When I use this code :

Code: Select all

Call :TimedChoice 12 6 SpecificLabelIfChoiceIsEntered SpecificLabelIfTimeRunsOut

to call the Timed Choice function it works just fine because there only 2 choices to choose from.

But when I add another choice to choose from, it doesn't stop the console for any moment and just proceeds to the "SpecificLabelIfTimeRunsOut"


I have modified misol's code a bit. Misol's code is now as follows :

Code: Select all

set chc=%~1
set cnt=%~2
set chk_chc=%~3
set timeovr=%~4
:timechc
set msg="Please enter the number of your choice in %cnt% sec.:      "
if "%cnt%"=="0" (goto :%timeovr%)
if not "%cnt%"=="0" choice /C !chc!0 /N /T 1 /D 0 /M %msg%
if %ErrorLevel%==3 gotoxy k /1 %msg% & set /a cnt-=1 & goto :timechc
goto :%chk_chc%


I don't know if the problem is occurring due to the modification or not.


Any help is appreciated!



Thanks,
PaperTronics

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to display the choice /t timer?

#15 Post by miskox » 19 Jul 2017 07:19

Code: Select all

set chc=%~1
set cnt=%~2
set chk_chc=%~3
set timeovr=%~4
:timechc
set msg="Please enter the number of your choice in %cnt% sec.:      "
if "%cnt%"=="0" (goto :%timeovr%)
if not "%cnt%"=="0" choice /C !chc!0 /N /T 1 /D 0 /M %msg%
if %ErrorLevel%==3 gotoxy k /1 %msg% & set /a cnt-=1 & goto :timechc
goto :%chk_chc%


Help for Choice /D:

Code: Select all

/D    choice        Specifies the default choice after nnnn seconds.
                       Character must be in the set of choices specified
                       by /C option and must also specify nnnn with /T.


You have 0 (zero) as your default value. It must be included in the list (for /C). Also ERRORLEVEL returns offset so in your

Code: Select all

if %ErrorLevel%==3 gotoxy k /1 %msg% & set /a cnt-=1 & goto :timechc


number 3 must be calculated correctly (how many arguments are there).

Saso

Post Reply