0

バッチ ファイルで BatteryStatus WMI クエリを使用してバッテリの状態を視覚化しようとしていますが、スクレイピングに問題があります。

: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

この「モジュール」は他のコードから呼び出されます。最後に、 BSTATUS変数もBCHK変数も定義されていません。BCHKは 1 または 0 である必要があります。何が間違っていますか?

4

1 に答える 1

1
: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
于 2013-11-25T06:59:22.677 に答える