Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file WMIC BatteryStatus刮取FOR循环,自定义错误消息不工作_Batch File_Wmi_Battery_Wmic - Fatal编程技术网

Batch file WMIC BatteryStatus刮取FOR循环,自定义错误消息不工作

Batch file WMIC BatteryStatus刮取FOR循环,自定义错误消息不工作,batch-file,wmi,battery,wmic,Batch File,Wmi,Battery,Wmic,我试图在批处理文件中使用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 pa

我试图在批处理文件中使用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。我做错了什么

: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