So I want to make a pc uptime storer to store the uptime, startuptime and the date inside of a json file. I wanted to make it in python so I came up with this:
import ctypes
import json
import datetime
import wmi
lib = ctypes.windll.kernel32
t = lib.GetTickCount64()
t = int(str(t)[:-3])
mins, sec = divmod(t, 60)
hour, mins = divmod(mins, 60)
days, hour = divmod(hour, 24)
wmiob = wmi.WMI()
sdata = wmiob.Win32_PerfFormattedData_PerfOS_System()
uptime = sdata[-1].SystemUpTime
tnow = datetime.datetime.now()
utime = datetime.timedelta(seconds=int(uptime))
boot = tnow-utime
bootime = "{}:{}:{}".format(boot.hour, boot.minute, boot.second)
with open("data.json", "r") as f:
data = json.load(f)
f.close()
data["uptime"].append(str(datetime.date.today()) + " : " + f"{days} days, {hour:02}:{mins:02}:{sec:02}" + " , " + "StartupTime : " + bootime)
with open("data.json", "w") as f:
json.dump(data, f, indent=4)
The json file looks like this:
{"uptime": []}
I wanted to run the python file on logout using gpedit.msc so i created a .bat file
The bat file code:
@echo off
python "F:\Program Folders\py projects\UptimeMonitor\main.py"
But it kept opening a terminal so i used a vbs:
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
oShell.CurrentDirectory = "F:\Program Folders\py projects\UptimeMonitor"
oShell.Run "cmd /c uptimestorer.bat", 0, false
WScript.Sleep(500)
It worked perfectly when i run the vbs manually anywhere in my pc but when i added it to gpedit shutdown, it just didn't work without an error.
If there's an alternate way to do this then please tell me or if there is any modifications needed in my code then please tell me that also here.
Remember I am using Windows