我正在尝试在我的批处理文件中使用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.我做错了什么?
答案 0 :(得分: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