0

I am creating a program that needs to countdown seconds. I used the choice command with /t 1 and /d to do that and also need to check if the user pressed any button on the keyboard.

I've got a problem here because I cant pass any non-keyboard character to /d .. so someone could press character from my allchoices string and it will stop counting down.

As far as I know - choice command could operate on a-z, A-Z, 0-9 and ASCII values of 128 to 254, but when I enter ascii like 128 (Ç) i get ERROR: Invalid syntax. /D only accepts single character.

So how could I work around this problem?

This is part of my code:

set /a time = 20

set "if_1=8"
set "if_2=10"
set "if_3=12"

set "allchoices=123456789abcdefghijklmnopqrstuvwxyz"

:loop 
    cls
    
    echo press 1 for %if_1% Threads
    echo press 2 for %if_2% Threads
    echo press 3 for %if_3% Threads
    echo press any other number key to exit
    echo.
    echo Countdown - [%time%]
    
    if %time%==0 goto launch

    choice /t 1 /c %allchoices% /d z >nul
    if %errorlevel% EQU 35 goto dec
    if %errorlevel% EQU 1 goto 1
    if %errorlevel% EQU 2 goto 2
    if %errorlevel% EQU 3 goto 3
    if %errorlevel% GEQ 4 goto end
    
:dec
    set /a time = %time%-1
    goto loop

and this is what I'm talking about:

set "allchoices=123456789abcdefghijklmnopqrstuvwxyzÇ"

choice /t 1 /c %allchoices% /d Ç >nul
if %errorlevel% EQU 36 goto dec
dan1st
  • 12,568
  • 8
  • 34
  • 67
matheo9
  • 31
  • 2
  • I can't replicate your problem. – Squashman Jul 02 '21 at 17:42
  • 2
    To begin with, do not overwrite the `%TIME%` variable, choose another name for it. Also what type of user do you expect to be able to input a keypress within one second of being prompted for one? – Compo Jul 02 '21 at 17:43
  • for what you are trying to achieve - a menu with a timer, [take a look here](https://stackoverflow.com/a/66399110/12343998) – T3RR0R Jul 02 '21 at 17:47
  • 5
    I am betting you saved the .bat file as UTF instead of ANSI. If it saved it as UTF the `Ç` becomes a 2 byte character. – Squashman Jul 02 '21 at 17:50
  • @Squashman that was it! thank you so much, now it works as I wanted – matheo9 Jul 02 '21 at 21:03

0 Answers0