JSI Tip 7135. How do I retrieve a computer's up time in a batch?

Using Uptime.exe, which runs on any Windows NT-based operating system (currently Windows NT 4.0, Windows 2000 (NT 5.0), Windows XP (NT 5.1), and Windows Server 2003 (NT 5.2)), I have scripted UpTimeMinutes.bat to return the number of minutes since the computer was restarted, when run on NT 5.0 or later.

The syntax for using UpTimeMinutes.bat is:

call UpTimeMinutes Minutes

where Minutes is a call directed numeric environment variable that will contain the number of minutes since the computer was restarted.

UpTimeMinutes.bat contains:

@echo off
if \{%1\}==\{\} @echo Syntax: UpTimeMinutes Minutes&goto :EOF
set line1=N
for /f "Tokens=6,8,10" %%t in ('uptime') do if "!line1!" EQU "N" set line1=Y&set /a days=%%t&set /a hours=%%u&set /a mins=%%v
set /a up=(%days% * 1440) + (%hours% * 60) + %mins%
endlocal&set /a %1=%up%

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.