windows 如何获得Batch中的电池百分比?

pdtvr36n  于 2023-03-24  发布在  Windows
关注(0)|答案(4)|浏览(172)

所以,我只是想知道如何得到电池的百分比在批处理.
我想如果格式是这样的话会很好:

:forever
get-battery
if "%battery%"=="100%" goto reached100
goto forever

:reached100  
echo Your battery has finished charging!
goto forever
r1wp621o

r1wp621o1#

科学家_7的答案应该被标记为正确。
当然,没有法律禁止从批处理中调用powershell。

powershell -command "(Get-WmiObject Win32_Battery).EstimatedChargeRemaining"
56
ylamdve6

ylamdve62#

使用WMIC

:: 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"
)

检查http://www.robvanderwoude.com/files/battrun_xp.txt上的完整脚本

sczxawaw

sczxawaw3#

你可以通过Wmic命令检查它。

编辑:

: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
ljo96ir5

ljo96ir54#

除此之外,我还需要确保批处理脚本不会在电池电量不足的笔记本电脑上运行,但也适用于没有电池的台式机。

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)

因此,这将电池变量设置为100,然后用实际的电池值覆盖它,这样台式机将报告100。/Value后的奇怪位只是为了抑制台式机上的电池设备未找到错误。

相关问题