|
本帖最后由 Myptour 于 2024-11-8 20:43 编辑
已知:powercfg /batteryreport 可以查看系统电池信息,
问题:需要在改变充电状态大约10秒甚至20秒过后才能刷新出最新状态
目的:有其他方法可以实时刷新充电状态的命令吗,最好是脚本文件
以下自己琢磨的,谢谢各位指点
@echo OFF
del battery-report.html>nul 2>nul
del battery.ini>nul 2>nul
del n.ini>nul 2>nul
powercfg /batteryreport
setlocal EnableDelayedExpansion
cd /d %~dp0
set n=0
For /f "tokens=1* delims=:" %%i in ('Type battery-report.html^|Findstr /n /c:"Report generated"') do (
Set /a n=%%i+1
ECHO !n!>>n.ini
)
endlocal
set /p number=<n.ini
set /a numa=%number%-1
set /a numb=%number%+2
call :checknum %numa% %numb%
findstr "Battery" battery.ini &&echo fail>result.log||echo pass>result.log
exit
:checknum
setlocal enabledelayedexpansion
for /l %%m in (%1,1,%2) do (
set lineFlag=0
for /f "delims=" %%r in ('type battery-report.html') do (
set /a lineFlag+=1
if !lineFlag!==%%m (
set lineContent=%%r
set lineContent=!lineContent: =!
echo.!lineContent!>>battery.ini
)
)
)
endlocal
goto :eof================================================================
想复杂了,有现成代码,如下:
@echo off
wmic path Win32_Battery Get BatteryStatus | find /v "BatteryStatus" | find "2" >nul 2>&1
if "%errorlevel%" == "0" (echo 电源供电,交流供电) else (echo 电池供电,直流供电)
echo.
pause
|
|