Search code examples
batch-filecmdwmic

WMIC LastBootUptime format problem, total number of days required


I am trying to calculate the last time the computer was restarted with CMD.

But I don't want it in Year/month/day format.

Send the total number of days, hours and minutes

Here's what I've been able to do so far:

setlocal 
 for /f "skip=1" %%i in ('wmic /node:Computer /USER / os get lastbootuptime') do (
  set string=%%i 
  goto next 
 ) 
 :next 
 echo Son Restart: %string:~6,2%/%string:~4,2%/%string:~0,4%---%string:~8,2%:%string:~10,2%

Output: Son Restart: 17/11/2023---02:33

But what I need: 2 Days - 8 Hours - 43 Minutes

Note: I should only use CMD, no powershell

Thank you very much for your support in advance.


Solution

  • If you're happy to use WMIC, then might I offer an alternative method:

    @Echo Off
    SetLocal EnableExtensions
    Set "s="
    For /F EOL^=S %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path
     Win32_PerfFormattedData_PerfOS_System Get SystemUptime  2^>NUL
     ') Do Set /A s=%%G, d=s / 86400, s %%= 86400, h=s / 3600,^
     s %%= 3600, m=s / 60, s=s %% 60 2>NUL
    If Defined s (Echo %d% Days - %h% Hours - %m% Minutes
        Pause)
    EndLocal
    Exit /B
    

    Of course you have the option of suffixing - %s% Seconds to the output, should you require that too.