2

I am using windows 8 to write a batch file and I got stuck in implementing the timer in batch file. I want to ask input from the user and give them one minute to type their input. Once the time hit a minute then display message like 'the time is over'. So, the time will start from 1 second and ends at 60 seconds OR start from 60 seconds and going down to 0 second. Either works just fine. Additionally, I want timer to be displayed somewhere on screen so that they can see the countdown. Also, while the timer is running I want the user to be able to type a word and hit enter. This program will not make user wait, but it will wait until the time is over OR as soon as the user enter a word (whichever comes first). After they enter a valid word then I want to store that word in certain variable and do something like (goto VALIDWORD OR echo That is a valid word!) I don't know if this is possible in batch file and there are more advanced language to use, but I want to complete this program using batch scripting. Thank you. Following is my concept:

@echo off
:Start
color EC
set /a time =60

:loop
cls
if %time% EQU 0 goto Timesup
if %time% LEQ 60 goto CONTINUE

:CONTINUE
ping localhost -n 2 >nul
set /a time-=1
set /p cho=     Enter your word: 
echo Remaing Time: %time%
goto loop

:Timesup
echo The time is over!
exit

Any ideas or support would be appreciated. Again Thanks!

  • possible duplicate of [How to make the SET command not wait for answer?](http://stackoverflow.com/q/24064970/2152082) – Stephan Jun 10 '15 at 10:32
  • I suggest you to change topic title to something like "Implementing timed input in batch file". The important keyword here is _input_. – Aacini Jun 10 '15 at 12:46

1 Answers1

2

Batch files are not designed for tasks like this one, but it is possible to perform certain advanced managements although in a limited manner. The subroutine below use choice command to get input keys, so it does not allow to end the input with Enter key nor to delete characters with BackSpace; it use 0 and 1 keys for such tasks.

@echo off
setlocal

call :ReadTimedInput 60 word=
echo Word read: "%word%"
goto :EOF


:ReadTimedInput seconds result=
setlocal EnableDelayedExpansion

set /A seconds=100+%1
set "letters=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"

echo Enter a word; press 0 to End, press 1 to Clear
:clear
set /P "=X!CR!                                     !CR!" < NUL
set "result="
:loop
   set /P "=X!BS! !CR!%seconds:~-2%: %result%" < NUL
   choice /C ¡10%letters% /N /CS /T 1 /D ¡ > NUL
   if %errorlevel% equ 1 goto tick
   if %errorlevel% equ 2 goto clear
   if %errorlevel% equ 3 echo/& goto endInput
   set /A char=%errorlevel%-4
   set "result=%result%!letters:~%char%,1!"
   :tick
   set /A seconds-=1
if %seconds% gtr 100 goto loop
echo !CR!00
set "result=Time out"
:endInput
endlocal & set "%2=%result%"
exit /B
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • @ Aacini: This almost answer my question, but I forget to be more clear. Does your code allows me to store the user input into certain variable and use it later. If so **which variable is storing the user's input**? I want to take the user's word and analyze to see if it is actually a valid. I have the code that validate the user's word but I need to store the user's word into some variable so that I can do something different. – Chhandra Aadhikary Jun 11 '15 at 03:27
  • "which variable is storing the user's input? " hmmm - `%word%`? Or whatever you specify with `call :ReadTimedInput 60 word=` – Stephan Jun 11 '15 at 07:38
  • Code segments designed to be used in a program are usually extracted into a _subroutine_ that is used in the way shown in the _subroutine header_, for example: `:ReadTimedInput seconds result=`. Previous line means that the subroutine must be called with a number of _seconds_ first followed by the name of the variable that receive the _result_. The example shown `call :ReadTimedInput 60 word=` means: _wait 60 seconds and store the word read in the variable "word"_. Another example: `call :ReadTimedInput 38 cho` means _wait 38 seconds and store the input in the variable "cho"_... – Aacini Jun 11 '15 at 11:31
  • @ Aacini: I tried printing the `%word%` variable using `pause` to see what is stored in word variable and it came out as "Time out". So whatever I enter the input, the `%word%` variable always contain "Time out" string. Following is what I got in CMD: `Enter a word; press 0 to End, press 1 to Clear 00: schedule Word read: "Time out" Press any key to continue . . .` {So, the `%word%` variable does not actually include the user's input. Is there a way to fix it? I tried changing the "Time out" into `%word%` and it shows the word read was empty, just "".} – Chhandra Aadhikary Jun 13 '15 at 16:13
  • Are you pressing _the zero digit_ to end the input, as "press 0 to End" instruction indicated? Run the program and enter: `schedule0` – Aacini Jun 13 '15 at 16:22
  • Oh that is right. Perfect. One more thing... What if I want to display the message if the user is out of time? Where in the code should I change to add the message (The message is in :TIMEOUT section)? I tried taking `exit /B` and it worked if they are out of time, but if they enter a word then it will continue to timeout message. For example, I replaced `exit /B` to `goto :TIMEOUT` and TIMEOUT basically display "You are out of time." BUT even if I enter a word it will goto TIMEOUT section. Is there a way to fix this? – Chhandra Aadhikary Jun 13 '15 at 20:23
  • Insert your `echo You are out of time` command between `echo !CR!00` and `set "result=Time out"` lines... May I ask you to select (check-mark) my answer? – Aacini Jun 13 '15 at 22:47