-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatteryLogger.bat
More file actions
58 lines (47 loc) · 1.44 KB
/
BatteryLogger.bat
File metadata and controls
58 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@echo off
setlocal
:: Set log file path
set "logFile=%USERPROFILE%\Documents\BatteryLog.txt"
:: Get readable timestamp: Tuesday, 5 August 2025, 10:13 PM
for /f "tokens=1-7 delims=/:. " %%a in ('echo %date% %time%') do (
set "weekday=%%a"
set "day=%%b"
set "month=%%c"
set "year=%%d"
set "hour=%%e"
set "minute=%%f"
set "second=%%g"
)
:: Handle AM/PM and 12-hour conversion
set "ampm=AM"
if %hour% GEQ 12 (
set "ampm=PM"
if %hour% GTR 12 set /a hour-=12
)
if %hour%==0 set hour=12
if %hour% LSS 10 set hour=0%hour%
set "datetime=%weekday%, %day% %month% %year%, %hour%:%minute% %ampm%"
:: Get battery status
for /f "skip=1" %%a in ('wmic path Win32_Battery get BatteryStatus') do (
set "batteryStatus=%%a"
goto :next1
)
:next1
:: Get battery percentage
for /f "skip=1" %%a in ('wmic path Win32_Battery get EstimatedChargeRemaining') do (
set "batteryPercent=%%a"
goto :next2
)
:next2
:: Clean values (remove spaces)
set "batteryStatus=%batteryStatus: =%"
set "batteryPercent=%batteryPercent: =%"
:: Interpret battery status
set "statusText=Unknown"
if "%batteryStatus%"=="1" set "statusText=Discharging"
if "%batteryStatus%"=="2" set "statusText=AC Power (Charging)"
if "%batteryStatus%"=="3" set "statusText=Fully Charged"
:: Write to log file
echo [%datetime%] Battery: %batteryPercent%%, Status: %statusText% >> "%logFile%"
echo Logged: [%datetime%] Battery: %batteryPercent%%, Status: %statusText%
endlocal