Page 1 of 2
How to display the choice /t timer?
Posted: 10 Jul 2017 05:47
by PaperTronics
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
Re: How to display the choice /t timer?
Posted: 10 Jul 2017 06:59
by miskox
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
Re: How to display the choice /t timer?
Posted: 11 Jul 2017 03:03
by PaperTronics
@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,
Re: How to display the choice /t timer?
Posted: 11 Jul 2017 04:52
by miskox
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
Re: How to display the choice /t timer?
Posted: 11 Jul 2017 05:53
by PaperTronics
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
Re: How to display the choice /t timer?
Posted: 11 Jul 2017 10:30
by misol101
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
Re: How to display the choice /t timer?
Posted: 11 Jul 2017 10:37
by Aacini
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
Re: How to display the choice /t timer?
Posted: 11 Jul 2017 21:39
by PaperTronics
@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.
PaperTronics
Re: How to display the choice /t timer?
Posted: 12 Jul 2017 01:35
by misol101
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.
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.
Re: How to display the choice /t timer?
Posted: 12 Jul 2017 04:30
by PaperTronics
@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
Thanks,
PaperTronics
Re: How to display the choice /t timer?
Posted: 12 Jul 2017 06:32
by misol101
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)
Re: How to display the choice /t timer?
Posted: 12 Jul 2017 06:47
by Aacini
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...
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
Re: How to display the choice /t timer?
Posted: 12 Jul 2017 23:18
by PaperTronics
@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
Re: How to display the choice /t timer?
Posted: 19 Jul 2017 02:58
by PaperTronics
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
Re: How to display the choice /t timer?
Posted: 19 Jul 2017 07:19
by miskox
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