BATCH - 如何仅从多行命令输出中获取一行



状态栏中的"电池"图标不起作用,我使用Windows批处理脚本检查电池电量。(我不想修复图标。但是,输出有点丑陋,没有空格,下一行有一个普通的数字。

那么,我该如何转换

EstimatedChargeRemaining
83

Battery Level is at 83

(或任何类似的东西。

PS:我使用的命令是WMIC PATH Win32_Battery Get EstimatedChargeRemaining

尝试使用subprocess&re模块。

前任:

import re
import subprocess
s = subprocess.check_output("WMIC PATH Win32_Battery Get EstimatedChargeRemaining")
print("Battery Level is at {0}".format(re.findall("d+", s)[0]))

输出:

Battery Level is at 83
for /f "tokens=* delims=" %%a in ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining /format:value') do (
for %%# in ("%%a") do set "%%#"
)
echo Battery Level is at %EstimatedChargeRemaining%

以下内容对我有用,是对@npocmaka答案的修改(谢谢,npocmaka(。

FOR /F "Tokens=1,* Delims==" %%A in (
'wmic PATH Win32_Battery get EstimatedChargeRemaining  /Format:list ^| FINDSTR "[0-9]"'
)DO (echo Battery Level is at %%B)

我更改了什么:
FINDSTR将输出从Unicode转换为ANSI以进行解析FOR /F

最新更新