title: How to get PC uptime in batch?
body: <p>I have a script here that will give me an uptime but it is not the correct up time and I dont really know where the error is or if this is even the best way to do it but it is what I have.  End game I want to set the uptime to a variable and if greater than x I will do a reboot or promt the user for a reboot and I will put this script in under the task scheduler.  Either way here is what I have.  Hope it is sufficient enough and I dont get scaled too bad over a stupid question.</p>
  
  <pre><code>@echo off 
  setlocal ENABLEEXTENSIONS
  call :Uptime d h n s
  echo/Uptime is: %d% days, %h% hours, %n% minutes, %s% seconds.
  pause
  goto :EOF
  
  :Uptime days hours mins [secs]
  setlocal ENABLEEXTENSIONS &amp; set "c=net statistics work"
  set t=2&amp;if "%date%z" LSS "A" set t=1
  for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do (
    for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do (
      set %%a=%%d&amp;set %%b=%%e&amp;set %%c=%%f))
  for /f "tokens=5-8 delims=:. " %%a in ('echo/^|time') do (
    set "hh=%%a" &amp; set "nn=%%b" &amp; set "ss=%%c")
  if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
  set /a dd=100%dd%%%100,mm=100%mm%%%100
  set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  if 1%hh% LSS 20 set hh=0%hh%
  set /a hh=100%hh%%%100,nn=100%nn%%%100,f=j*1440+hh*60+nn
  for /f "tokens=3-8 delims=/:M " %%a in ('%c%^|findstr/b /c:"Stat"') do (
    set mm=%%a&amp;set dd=%%b&amp;set yy=%%c&amp;set hh=%%d&amp;set nn=%%e%%f)
  if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
  set /a dd=100%dd%%%100,mm=100%mm%%%100
  set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  if 1%hh% LSS 20 set hh=0%hh%
  if {%nn:~2,1%} EQU {P} if "%hh%" NEQ "12" set hh=1%hh%&amp;set/a hh-=88
  if {%nn:~2,1%} EQU {A} if "%hh%" EQU "12" set hh=00
  if {%nn:~2,1%} GEQ {A} set nn=%nn:~0,2%
  set /a hh=100%hh%%%100,nn=100%nn%%%100,s=j*1440+hh*60+nn,n=f-s
  set /a d=n/1440,n%%=1440,h=n/60,n%%=60
  endlocal &amp; set "%1=%d%" &amp; set "%2=%h%" &amp; set "%3=%n%" &amp; (if "%4" NEQ "" set "%4=%ss%") &amp; goto :EOF
  </code></pre>