解析WMIC输出 [英] Parsing WMIC output

查看:600
本文介绍了解析WMIC输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是应该读取当前屏幕分辨率变量BAT脚本。我得到的 WMIC desktopmonitor获取屏幕宽度/值命令屏幕分辨率(宽度,高度是一样的)。

输出示例:

  C:\\用户\\ Pietu1998> WMIC desktopmonitor得到屏幕宽度/值
屏幕宽度= 1920
屏幕宽度=
C:\\用户\\ Pietu1998>

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

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

 设置屏幕宽度=FOR / F跳过= 2令牌= *%% f由于('WMIC desktopmonitor得到屏幕宽度/值')做(
    如果%屏幕宽度%EQU(
        回声行:%%˚F
        设置屏幕宽度= %%˚F
        回声VAR:%屏幕宽度%
        设置屏幕宽度=%屏幕宽度:〜12%
    )

我正确地得到输出,因为该线是由第一印刷回声,但由于某些原因,第二个回声输出什么。该生产线是不是放在变量。

我缺少的是在这里吗?我一直在谷歌上搜索一下2小时了。

更新:我发现使用 FINDSTR 办法和临时文件。

  WMIC desktopmonitor获取屏幕宽度/值| FINDSTR屏幕宽度=。 > %TEMP%\\ tmp目录
集/ p屏幕宽度=< %TEMP%\\ tmp目录
德尔%TEMP%\\ tmp目录


解决方案

回声VAR:%屏幕宽度%< ---你不能设置和使用变量如果没有一些特殊技术的循环,就像推迟扩张之内。

这是不是你所需要的?

 关闭@echo
设置屏幕宽度=
FOR / F令牌= 2 delims ==%% f由于('WMIC desktopmonitor得到屏幕宽度/值')做(
    如果没有定义屏幕宽度设置屏幕宽度= %%˚F

暂停

I have a BAT script that is supposed to read the current screen resolution to variables. I get the screen resolution with the wmic desktopmonitor get screenwidth /value command (for the width, the height is the same thing).

Example output:

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


ScreenWidth=1920


ScreenWidth=




C:\Users\Pietu1998>

I have two monitors, so it only shows the resolution for the one in use.

I have tried using a for loop to skip the first two empty lines and then read the data.

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%
    )
)

I am getting the output correctly, because the lines are printed by the first echo, but for some reason the second echo outputs nothing. The line is not put in the variable.

What am I missing here? I've been googling about it for 2 hours now.

UPDATE: I found a way using findstr and a temporary file.

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

解决方案

echo var: %screenwidth% <--- You can't set and use a variable within a loop without some special techniques, like delayed expansion.

Does this do what you need?

@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

这篇关于解析WMIC输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆