【问题标题】:Parsing WMIC output解析 WMIC 输出
【发布时间】:2014-05-18 17:43:17
【问题描述】:

我有一个 BAT 脚本,它应该将当前屏幕分辨率读取为变量。我使用wmic desktopmonitor get screenwidth /value 命令获得屏幕分辨率(宽度和高度相同)。

示例输出:

C:\Users\Pietu1998>wmic desktopmonitor get screenwidth /value


ScreenWidth=1920


ScreenWidth=




C:\Users\Pietu1998>

我有两台显示器,所以它只显示正在使用的一台的分辨率。

我尝试使用for 循环跳过前两个空行,然后读取数据。

set screenwidth=

for /F "skip=2 tokens=*" %%F in ('wmic desktopmonitor get screenwidth /value') do (
    if "%screenwidth%" equ "" (
        echo line: %%F
        set screenwidth=%%F
        echo var: %screenwidth%
        set screenwidth=%screenwidth:~12%
    )
)

我得到了正确的输出,因为这些行是由第一个 echo 打印的,但由于某种原因,第二个 echo 没有输出任何内容。该行没有放在变量中。

我在这里缺少什么?我已经在谷歌上搜索了 2 个小时。

更新:我找到了一种使用findstr 和临时文件的方法。

wmic desktopmonitor get screenwidth /value | findstr "ScreenWidth=." > %temp%\tmp
set /P screenwidth=< %temp%\tmp
del %temp%\tmp

【问题讨论】:

    标签: windows parsing batch-file output


    【解决方案1】:

    echo var: %screenwidth%

    这能满足你的需要吗?

    @echo off
    set "screenwidth="
    for /F "tokens=2 delims==" %%F in ('wmic desktopmonitor get screenwidth /value') do (
        if not defined screenwidth set screenwidth=%%F
    )
    pause
    

    【讨论】:

    • 不,screenwidth 被设置为 %%F(字面意思)。
    • 这里没有这样做。如图所示粘贴了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2020-12-17
    相关资源
    最近更新 更多