【问题标题】:How do I get the battery percentage in Batch?如何批量获取电池百分比?
【发布时间】:2014-11-07 00:48:05
【问题描述】:

所以,我只想知道如何批量获取电池百分比。
我觉得如果格式是这样就好了:

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

:reached100  
echo Your battery has finished charging!
goto forever

【问题讨论】:

    标签: windows batch-file cmd batterylevel


    【解决方案1】:

    scientist_7 的答案应标记为正确。

    当然,没有法律禁止从批处理中调用 powershell。

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

    【讨论】:

    • 对于 Powershell 6 及更高版本(即 Powershell Core),您可以使用(Get-CimInstance Win32_Battery).EstimatedChargeRemaining。有关 Get-CimInstance 的更多详细信息,请访问 stackoverflow.com/questions/54495023/…
    【解决方案2】:

    使用 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查看完整的脚本

    【讨论】:

    • 我永远不会明白为什么人们更喜欢以一种不寻常的方式写arrays in Batch files,这给他们一种奇怪而神秘的外观,经常需要解释,而不是使用方括号标准方式,即全球通用:SET BatteryStatus[1]=discharging;见:this comment.
    • 我想要百分比。喜欢:100 (100%) 或 2 (2%),所以我可以在“IF”命令中使用它。
    • 代替BatteryStatus,试试EstimatedChargeRemaining
    • 另外 - 我对此很困惑。
    【解决方案3】:

    您可以通过 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
    

    【讨论】:

    • 查看我的新更改。
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 2018-11-17
    相关资源
    最近更新 更多