Solved Variable filtering and time-math. please help

Hewlett-packard / Pavilion hpe
December 31, 2014 at 19:36:15
Specs: Windows 7, 3.6GHz 16GB
I am trying to make an up-time clock in batch. It gets the last boot time and matches it with the current time to create an up-time variable.

I am using this to get the boot time and current time:
wmic os get LastBootUpTime /format:value
wmic os get LocalDateTime /format:value

They both return an output like this:
where "+060" would be the extra amount of minutes to be added, because time sones.

However, when I try to filter the variable into one containing the date, and one containing the time using this:

for /f "tokens=2 delims==" %%A in ('wmic os get LastBootUpTime /format:value') do set lastBoot=%%A
for /f "tokens=2 delims==" %%A in ('wmic os get LocalDateTime /format:value') do set localTime=%%A
set lastBoot1=!lastBoot:~0,-18!
set lastBoot2=!lastBoot:~0,8!

I end up with lastBoot1 and lastBoot2 having the same value.
(I didn't do the localDate calculation because it works the same way)

Also, how would I do "time-math" to get the amount of time (in days, hours and minutes) since the last boot? (time-math does silly things with my brain)


If infinity is infinite, is the square root of infinity infinite as well?
- Kurp Von Steiner

message edited by RainBawZ

See More: Variable filtering and time-math. please help

Report •

December 31, 2014 at 21:12:46
Time math [and date math] in batch almost always end in tears.
But there are helpers here who can do it in VBS.

M2 Golden-Triangle

Report •

January 1, 2015 at 21:30:46
✔ Best Answer
Here's a feeble effort (using vbscript, as was wisely advised):
@echo off & setlocal
for /f "tokens=2 delims==" %%a in ('\wmic os get lastbootuptime /format:value') do set d1=%%a
echo raw date-time: %d1%
set t=%d1:~8,2%:%d1:~10,2%:%d1:~12,2%
set d1=%d1:~0,4%/%d1:~4,2%/%d1:~6,2%
echo formatted date: %d1%
echo formatted time: %t%
if not exist ddiff.vbs (
:: builds the vbscript if not existant
>ddiff.vbs echo wscript.echo datediff^(wscript.arguments^(0^), wscript.arguments^(1^), cstr^(date^)+" "+cstr^(time^)^)
:: h for hours, n for minutes, s for seconds, d for days in foll. line
for /f %%a in ('cscript ddiff.vbs s "%d1% %t%"') do set dif=%%a
echo captured difference: %dif%
::---------- convert from seconds to d h:m:s Left all debugging verbiage in this part
(set dlm= days, )
for %%z in (86400 3600 60) do (
echo %%z
set /a t=dif/%%z
echo t:!t!
set k=!k!!t!!dlm!
set /a dif=dif"%%"%%z
echo new dif: !dif!
set dlm=:
echo !k!%dif%
::======== end batchscript
"n" gets you minutes difference. I dispensed with the wmic localtime in favor of just letting the system provide that in the vbscript. The simple conversion routine should provide the units as needed.

ps: this was wrong for what you wanted:
set lastBoot1=!lastBoot:~0,-18!
you would want either:
~-18 or -18.-12
~8 or ~8,6
either way works since the string length is consistant.

message edited by nbrane

Report •
Related Solutions

Ask Question