How to create a batch file countdown timer in min:sec format that uses a variable as the finishing time and uses the current time as the start time?

26,901

Solution 1

@echo off
setlocal

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=14
set endM=58

title Timer
mode con cols=16 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 Timer:  %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
pause

Solution 2

What I would do is create a timeout function such as timeout /t xx where xx equals the time in seconds. If that is what you're looking for.

You can also do timeout /t xx >nul so that it won't display a message saying how many seconds you have left. If that is what you want then use the first one I showed. I hope this helped

Share:
26,901
user3506608
Author by

user3506608

Updated on April 24, 2020

Comments

  • user3506608
    user3506608 about 4 years

    I am trying to create a countdown timer that is in min:sec format which uses a variable taken from a text document and uses it as the finish time and uses the current time (time the .bat was started) as the start time. Currently I have this code which works and gets the time from the text document but I can't seem to figure out how to use get it to work.

    Code:

    @echo off
    
    set CurrentTime=%time:~0,2%.%time:~3,2%
    
    set /p StartTime=<"ResponseTime.txt"
    
    echo.
    
    echo %CurrentTime% %StartTIme%
    
    echo.