I am trying to use the BatteryStatus WMI Query in my batch file to visualize the battery status, now i am having problems with scraping it.

:GETBATLVL
REM : Get the Battery level via wmic
SET BCHK=
FOR /F "usebackq skip=1" %%C IN (`wmic path Win32_Battery get BatteryStatus /Value ^|     find /c "."`) DO (
REM If a "." is given it is part of an error message, so don't continue. Heres also the first problem
    SET "BCHK=%%C"
)
::FOR /F "usebackq skip=1" %%A IN (`wmic path Win32_Battery get BatteryStatus /Value`) DO (
REM the second problem
SET "BLVL=%%A"
IF NOT "!BLVL!"=="" (
IF "!BCHK!" GEQ "1" ( 
    SET "BATTERY_NOT_EXISTING=TRUE"
    SET "BSTATUS=No battery installed"
) ELSE IF "!BLVL!"=="1" (
    SET "BSTATUS=Discharging"
) ELSE IF "!BLVL!"=="2" (
    SET "BSTATUS=Battery full on AC"
) ELSE IF "!BLVL!"=="3" (
    SET "BSTATUS=Fully charged"
) ELSE IF "!BLVL!"=="4" (
    SET "BSTATUS=Battery low"
) ELSE IF "!BLVL!"=="5" (
    SET "BSTATUS=Battery critical low"
) ELSE IF "!BLVL!"=="6" (
    SET "BSTATUS=Charging"
) ELSE IF "!BLVL!"=="7" (
    SET "BSTATUS=Charging/Battery high"
) ELSE IF "!BLVL!"=="8" (
    SET "BSTATUS=Charging/Battery low"
) ELSE IF "!BLVL!"=="9" (
    SET "BSTATUS=Charging/Battery critical"
) ELSE IF "!BLVL!"=="10" (
    SET "BSTATUS=Undefined"
) ELSE IF "!BLVL!"=="11" (
    SET "BSTATUS=Partially charged"
)
)
)
EXIT /B

This "Module" is called by other code. Neither the BSTATUS nor the BCHK variable is defined in the end. BCHK should be 1 or 0. What am i doing wrong?

有帮助吗?

解决方案

:GETBATLVL
    REM : Get the Battery level via wmic
    SET "BCHK="
    wmic path Win32_Battery get BatteryStatus 2>nul | find /c "." > nul 
    IF NOT ERRORLEVEL 1 (
        REM If a "." is given it is part of an error message, so don't continue. Heres also the first problem
        REM If this code gets executed, a dot is found (no errorlevel on dot search). So there is a problem
        SET "BCHK=1"
        SET "BATTERY_NOT_EXISTING=TRUE"
        SET "BSTATUS=No battery installed"
        GOTO END_GETBATLVL
    )

    SET "BLVL="
    FOR /F "tokens=2 delims==" %%A IN ('wmic path Win32_Battery get BatteryStatus /Value ^| find "=" ') DO (
        SET "BLVL=%%A"
    )

    IF "%BLVL%"=="1" (
        SET "BSTATUS=Discharging"
    ) ELSE IF "%BLVL%"=="2" (
        SET "BSTATUS=Battery full on AC"
    ) ELSE IF "%BLVL%"=="3" (
        SET "BSTATUS=Fully charged"
    ) ELSE IF "%BLVL%"=="4" (
        SET "BSTATUS=Battery low"
    ) ELSE IF "%BLVL%"=="5" (
        SET "BSTATUS=Battery critical low"
    ) ELSE IF "%BLVL%"=="6" (
        SET "BSTATUS=Charging"
    ) ELSE IF "%BLVL%"=="7" (
        SET "BSTATUS=Charging/Battery high"
    ) ELSE IF "%BLVL%"=="8" (
        SET "BSTATUS=Charging/Battery low"
    ) ELSE IF "%BLVL%"=="9" (
        SET "BSTATUS=Charging/Battery critical"
    ) ELSE IF "%BLVL%"=="10" (
        SET "BSTATUS=Undefined"
    ) ELSE IF "%BLVL%"=="11" (
        SET "BSTATUS=Partially charged"
    ) ELSE (
        SET "BSTATUS=Unknown"
    )

:END_GETBATLVL
EXIT /B
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top