Need to create a batch script countdown timer synchronised to the system clock

2.8k Views Asked by At

I would like to create a batch file countdown timer (needs to be batch script due to restrictions in place). I've used two scripts so far that 'kind of' do the job, but have their own downsides.

The criteria is as follows: 1. Needs to show system time in xx:xx:xx format. 2. Needs to countdown in minutes + seconds in xx:xx format. 3. Needs to be able to work in 12 hour blocks (e.g. 7pm to 7am)

The countdown script I've been using (not my own creation, I've just modified certain sections) is as follows:

@echo off
setlocal

REM This script starts countdown from time you open batch file.
REM Open at 19:00:23 and it will finish at 07:00:23 (it's range is 60 seconds) 
REM This can be checked against http://www.rechnr.com/online-calculator/how-many-hours-left-until-tomorrow-calculator.html

:settime1

rem Get end time
REM for /F "tokens=1,2 delims=:" %%a in ("ResponseTime.txt") do set /A endH=10%%a%%100, endM=1%%b%%100

REM Just for testing:
set endH=07
set endM=00

title Timer
mode con cols=20 lines=2

:synchronize
for /F "tokens=1,2 delims=:" %%a in ("%time%") do set /A "minutes=(endH*60+endM)-(%%a*60+1%%b-100)-1, seconds=159"

:wait
timeout /T 1 /NOBREAK > NUL
echo Shift End:  %minutes%:%seconds:~-2%
set /A seconds-=1
if %seconds% geq 100 goto wait
set /A minutes-=1, seconds=159, minMOD5=minutes %% 5
if %minutes% lss 0 goto :buzz
if %minMOD5% equ 0 goto synchronize
goto wait

:buzz
mode con cols=100 lines=30
echo End of Shift
SET /P ANSWER=Would you like to shut down the machine (Y/N)? 
echo You chose: %ANSWER% 
if /i {%ANSWER%}=={y} (goto :yes) 
if /i {%ANSWER%}=={yes} (goto :yes) 
goto :no 
:yes 
echo Shutting down
shutdown /s /t 5 /c "Shutting down in 5 seconds"
pause > nul
exit /b 0 

:no 
echo You pressed no! 
pause > nul
exit /b 1

The downside to this code is that it doesn't synchronise to the system clock, thus it becomes out of sync, also it doesn't work before midnight for the night shift.

Any help refining this would be fantastic, unsure really how to use the system clock as the synchronise variable for countdown.

Thanks!

1

There are 1 best solutions below

1
On

Some time ago I modified a someone else's clock written in pure Batch and give it an alarm and timer, and several "skins". This is the help screen of such program:

Clock.bat [/D] [/12] [/C] [/S:#] [/B:#] [/T:m[:s]] [/A:hh:mm]

   /D        Set blinking dots
   /12       Set 12 hour format
   /C        Show calendar
   /S:#      Set small size, from 0 to 2 (cancel /Calendar)
   /B:#      Set brightness, from 0 to 3
   /T:m[:s]  Set timer in minutes and optional seconds
   /A:hh:mm  Set alarm time in 24 hour format (minutes in two digits)

You may run several instances of this program to set several timers or alarms
at different times; however, if more than one timer/alarm sounds at same time,
this program may fail.

To stop the buzzing alarm, press Enter.

This is the /Size:1 skin with counting timer, and also when the timer ends:

Clock.bat /S:1 with timer running Clock.bat /S:1 with timer buzzing

You may download such a program from this site.