:: Variables to translate the returned BatteryStatus integer to a descriptive text
SET BatteryStatus.1=discharging
SET BatteryStatus.2=The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging.
SET BatteryStatus.3=fully charged
SET BatteryStatus.4=low
SET BatteryStatus.5=critical
SET BatteryStatus.6=charging
SET BatteryStatus.7=charging and high
SET BatteryStatus.8=charging and low
SET BatteryStatus.9=charging and critical
SET BatteryStatus.10=UNDEFINED
SET BatteryStatus.11=partially charged
:: Read the battery status
FOR /F "tokens=*" %%A IN ('WMIC Path Win32_Battery Get BatteryStatus /Format:List ^| FIND "="') DO SET %%A
:: Check the battery status, and display a warning message if running on battery power
IF NOT "%BatteryStatus%"=="2" (
> "%~dpn0.vbs" ECHO MsgBox vbLf ^& "The laptop is currently running on its battery." ^& vbLf ^& vbLf ^& "The battery is !BatteryStatus.%BatteryStatus%!." ^& vbLf ^& vbLf ^& "Connect the laptop to the mains voltage if possible." ^& vbLf ^& " "^, vbWarning^, "Battery Warning"
CSCRIPT //NoLogo "%~dpn0.vbs"
DEL "%~dpn0.vbs"
)
:forever
Goto get-battery
Echo "%Ba%"
:Next
if "%Ba%"=="100" Goto Fin
goto forever
:Fin
echo Your battery has finished charging!
pause>nul
exit
:get-battery
for /f "tokens=2 delims==" %%E in ('wmic path Win32_Battery get EstimatedChargeRemaining /value') do (set "Ba=%%E")
Goto Next
SET BatteryPercent=100
FOR /F "tokens=2 delims==" %%i in ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Value 2^>^&1') DO SET BatteryPercent=%%i
IF %BatteryPercent% LSS 20 (EXIT)
4条答案
按热度按时间r1wp621o1#
科学家_7的答案应该被标记为正确。
当然,没有法律禁止从批处理中调用powershell。
ylamdve62#
使用WMIC
检查http://www.robvanderwoude.com/files/battrun_xp.txt上的完整脚本
sczxawaw3#
你可以通过Wmic命令检查它。
编辑:
ljo96ir54#
除此之外,我还需要确保批处理脚本不会在电池电量不足的笔记本电脑上运行,但也适用于没有电池的台式机。
因此,这将电池变量设置为100,然后用实际的电池值覆盖它,这样台式机将报告100。/Value后的奇怪位只是为了抑制台式机上的电池设备未找到错误。