Home > OS >  Is there a way i can make a counter in batch?
Is there a way i can make a counter in batch?

Time:11-29

So basically, I’m trying to make a code in BATCH that counts infinitely to show how long a computer has been running. How do i do it?

I inputted:

@echo off
:loop
set /a sec =1
echo Computer Runtime:
if %sec% geq 60 set /a min =1
if %min% geq 60 set /a hrs =1
echo %hrs%:%min%:%sec%
timeout /t 1 /nobreak >nul
goto loop

I got a window that ended itself instead.

I expected a running window that DID NOT end.

CodePudding user response:

Mmmm... This is the way I would do it:

@echo off
setlocal EnableDelayedExpansion

for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"
set /A hrs=0,min=100,sec=100

echo Computer Runtime:
:loop
set /A "sec=(sec-99)%`,min =^!sec-100,hrs =min/60,min=min%` 100,sec =100"
set /P "=%hrs%:%min:~1%:%sec:~1%!CR!" < NUL
timeout /T 1 /nobreak > NUL
goto loop
  • Related