Search code examples
batch-filetimerdecimalclockdigits

How can I add 0 before a numeric variable in batch


I can't seem to find the answer anywhere, even tho its such a "basic" thing I would assume someone else had already done it.

Anyways, here goes nothing.

I have made a simple batch script as a timer (counting up) and its in the dd:hh:mm:ss format but only displays what is necessary. For example if days and hours = 0 its only mm:ss etc.

Now I have ran into a slight issue when going from 0-9...

I want it to say 00-01-02-03-04-05... But right now its saying 0-1-2-3-4-5...

Lets say I put the time in a loop, with a 1 second delay always adding one second. I made a numeric /a variable since i'm working with numbers. But I can't seem to figure out how to add a 0 before the number if its less than 10...

So far I've tried the following:

echo %s:""=0%

Nevermind, I figured out the solution while typing this... See my answer


Solution

  • This is my final code, and it works because I just apply padding at all times if there is room for it.

    Simple solution to a simple problem.

    @echo off
    title Stream counter
    color a
    cd /d "%~dp0"
    set /a x=3
    set nl=^&echo.
    goto :top
    :retry
    cls
    if %x% LEQ 0 (
        exit()
        ) else (
        title OOPS!
        echo Oops! You either didn't press anything or waited too long,
        echo if you're still there you can try again in a few seconds.
        ping 127.0.0.1 -n 10 >nul
        )
    :top
    title Stream counter
    cls
    echo Attemps left^: %x%
    echo Start counter from zero? (y/n)
    choice /n /c YNC /t 10 /d C>nul
    if %errorlevel%==1 (
        goto :yes
        ) else if %errorlevel%==2 (
        goto :no
            ) else if %errorlevel%==3 (
            set /a x-=1
            goto :retry
            )
    :yes
    cls
    set /a d=0
    set /a h=0
    set /a m=0
    set /a s=0
    goto :start
    :no
    cls
    echo where do you want your timer to start?
    echo Start by entering day^(s^)
    set /p d=""
    echo Now enter hour^(s^)
    set /p h=""
    echo Then enter minute^(s^)
    set /p m=""
    echo And finally second^(s^)
    set /p s=""
    
    goto :start
    :start
    rem delete this if you want to keep the result (it will get overwritten anyways...)
    del final.txt
    rem Use the choice to ask if you want to continue, and use that as a "Press any key to stop", kind of thing.
    :startloop
    ::Check point "seconds"
    :cps
    if %s% geq 60 (
    set /a s-=60
    set /a m+=1
    goto :startloop
    )
    ::Check point "minutes"
    :cpm
    if %m% geq 60 (
    set /a m-=60
    set /a h+=1
    goto :startloop
    )
    ::Check point "hours"
    :cph
    if %h% geq 24 (
    set /a h-=24
    set /a d+=1
    goto :startloop
    ) else (
    goto :mainloop
    )
    :mainloop
    cls
    set ps=00%s%
    set pm=00%m%
    set ph=00%h%
    set ps=%ps:~-2%
    set pm=%pm:~-2%
    set ph=%ph:~-2%
    if %d% geq 1 (
        if %h% geq 1 set "output=%d% day^(s^) %ph%^:%pm%^:%ps%"
        if %h% equ 0 set "output=%d% day^(s^) %pm%^:%ps%"
    ) else if %h% geq 1 (
        set "output=%ph%^:%pm%^:%ps%"
    ) else (
        set "output=%pm%^:%ps%"
    )
    echo %output% > counter.txt
    echo Current time^: %output% &echo.
    echo Do you wish to abort? (Y/N)
    choice /n /c yn /t 1 /d n >nul
    if %errorlevel%==1 goto :end
    set /a s+=1
    ::Check point "seconds"
    :cps
    if %s% geq 60 (
    set /a s-=60
    set /a m+=1
    )
    ::Check point "minutes"
    :cpm
    if %m% geq 60 (
    set /a m-=60
    set /a h+=1
    )
    ::Check point "hours"
    :cph
    if %h% geq 24 (
    set /a h-=24
    set /a d+=1
    )
    goto :mainloop
    :end
    :: END ::
    rem COPY TO RESULT.txt and del counter.txt
    copy /V counter.txt final.txt & del counter.txt
    exit()