Search code examples
batch-filecountdown

More efficient countdown routine? [.BAT]


I'm trying to run a script that counts down from 60:00, where 60 is the number of seconds and 00 the number of milliseconds. I can't get the script to function properly at all. Anyone have any idea on what's the problem?

:PhaseTwo
Set /a bignum=60
Set smallnum=00
Set /a handlevariable=1
Set /a bignumhandle=0
ping localhost -n 2 >nul

:StopWatchRoutine
If %bignum% EQU 00 GoTo :StopWatchEndCheck
:StopWatchEndCheckNo
If %bignum% EQU 9 set /a bignumhandle=1
If %smallnum% EQU 9 set /a handlevariable=1
If %handlevariable% EQU 1 GoTo :NumberMods
Set /a smallnum-=1
GoTo :StopWatchHandle

:StopWatchEndCheck
If %smallnum% EQU 01 GoTo :StopWatchExit
GoTo :StopWatchEndCheckNo

:NumberMods
If %smallnum% EQU 00 set /a smallnum=99
If %smallnum% EQU 00 set /a handlevariable=0
If %smallnum% EQU 01 set smallnum=00
If %smallnum% EQU 02 set smallnum=01
If %smallnum% EQU 03 set smallnum=02
If %smallnum% EQU 04 set smallnum=03
If %smallnum% EQU 05 set smallnum=04
If %smallnum% EQU 06 set smallnum=05
If %smallnum% EQU 07 set smallnum=06
If %smallnum% EQU 08 set smallnum=07
If %smallnum% EQU 09 set smallnum=08
If %smallnum% EQU 9 set smallnum=09
If %smallnum% EQU 99 set /a bignum-=1
If %bignumhandle% EQU 1 GoTo :BigNumMods
GoTo :StopWatchHandle

:BigNumMods
If %bignum% EQU 01 set bignum=00
If %bignum% EQU 02 set bignum=01
If %bignum% EQU 03 set bignum=02
If %bignum% EQU 04 set bignum=03
If %bignum% EQU 05 set bignum=04
If %bignum% EQU 06 set bignum=05
If %bignum% EQU 07 set bignum=06
If %bignum% EQU 08 set bignum=07
If %bignum% EQU 09 set bignum=08
If %bignum% EQU 9 set bignum=09


:StopWatchHandle 
cls
echo Program Launch Console, Version 1.0.2
echo.
echo StopWatch: %bignum%:%smallnum%
echo.
ping localhost -n 1 >nul
GoTo :StopWatchRoutine

:StopWatchExit
echo.
GoTo :PhaseThree

I'm using this batch file as part of a user interface, so it is important that the echo part of it stays intact. Otherwise, any suggestions on how to make the script more efficient are welcome.

Thank you for all your assistance, it's very, VERY much appreciated.


Solution

  • Excuse me. A couple comments first:

    • I dont' like questions that state: "Hey, my code don't works. Please review it and fix the problem". You should explain what the code is supposed to do and what it really does.
    • I think you have a couple confusions here, starting with "and 00 the number of milliseconds" (milliseconds have 3 digits, centiseconds 2). If you want that the program refresh the screen as fast as possible (in order to show elapsed milliseconds, or centiseconds), why do you use ping command? Its effect is precisely the opposite to the desired one!

    Anyway, if you want a more efficient countdown routine in Batch, try this one:

    @echo off
    setlocal EnableDelayedExpansion
    
    
    :PhaseTwo
    
    rem Get a CR control character (Ascii 13)
    for /F %%a in ('copy /Z "%~F0" nul') do set "CR=%%a"
    
    cls
    echo Program Launch Console, Version 1.0.2
    echo.
    set /P "=StopWatch: 60.000!CR!" < NUL
    
    set /A bignum=60, smallnum=2000
    for /F "tokens=3 delims=:." %%a in ("%time%") do set "currentSec=%%a"
    
    :StopWatchRoutine
    for /F "tokens=3 delims=:." %%a in ("%time%") do if "%currentSec%" equ "%%a" (
       set /A smallnum-=4
    ) else (
       rem The next line will help to adjust previous smallnum's subtrahend
       REM ECHO/
       set "currentSec=%%a"
       set /A bignum-=1, smallnum=2000
       if !bignum! equ 0 goto StopWatchExit
    )
    
    :StopWatchHandle 
    set /P "=StopWatch: %bignum%:%smallnum:~1% !CR!" < NUL
    GoTo :StopWatchRoutine
    
    :StopWatchExit
    echo StopWatch: 0.000
    echo/
    echo.
    

    Note that, in order to display the milliseconds in a way as realist as possible, you must adjust the number in set /A smallnum-=4 command. The "4" is the right one for my computer.