解析输出时如何更正变量覆盖行为 [英] How to correct variable overwriting misbehavior when parsing output

查看:79
本文介绍了解析输出时如何更正变量覆盖行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在批处理文件中,我正在检查Baseboard信息,其中包括以下内容:
BaseboardCheck.cmd

In a batch file, I am checking for Baseboard information, with the following:
BaseboardCheck.cmd

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1,2* delims==" %%a in ('wmic baseboard get /format:list') DO ( 

    if ["%%a"] EQU ["Product"] (
        set PlatformInfo=%%b

        if defined PlatformInfo (
            echo.!PlatformInfo!
            echo.!PlatformInfo!This overwrites the variable
        )
    )

    if ["%%a"] EQU ["Version"] (
        set BaseboardVersion=%%b

        if defined BaseboardVersion (
            echo.!BaseboardVersion!
            echo.!BaseboardVersion!This overwrites the variable
        )
    )   
)

上面的问题:当回显出来时,变量将被覆盖,而不是附加到该变量.
输出:

The above problem: The variables get overwritten, rather than appended to, when echo'd out.
Output:

DX79SI
This overwrites the variable
AAG28808-600
This overwrites the variable

我想去的是:

DX79SI
DX79SIThis overwrites the variable
AAG28808-600
AAG28808-600This overwrites the variable

我已经花了几个小时(并将继续这样做),但希望其他人遇到此问题.并希望其他任何遇到此解析问题的人将来都能避免.

I have spent a few hours on this (and will continue to do so) but am hoping someone else has run into this issue. And am hoping anyone else who runs into this parsing problem can avoid it in the future.

由此产生的另一个问题是,它似乎破坏了条件逻辑.

The additional problem that comes out of this is that it seems to break conditional logic.

更新
在所有帮助之后,我想出了以下解决方案:

Update
After all of the assistance, I came up with this solution:

for /f "skip=2 tokens=1,2 delims=," %%a in ('wmic baseboard get Product^,Version^,Width /format:csv') do (

    set Platform=%%a
    set BaseboardVersion=%%b
)
echo.Platform: %Platform% 
echo.Version %BaseboardVersion%.

推荐答案

是的,您有问题,但这不是您的想法.

Yes, you have a problem, but it is not what you think.

wmic具有特殊的行为:在每行输出的末尾有一个附加的回车符,即每行以0x0d 0x0d 0x0a

wmic has a particular behaviour: at the end of each line output there is an aditional carriage return, that is, each line ends with 0x0d 0x0d 0x0a

此附加的回车符存储在变量中,当回显到控制台时,您将获得数据和回车符,因此,如果变量后面跟随有更多文本,则将光标定位在变量的开头行(回车),此文本将在先前回显的数据之上回显.

This aditional carriage return is stored inside your variable and, when echoed to console, you get the data and the carriage return, so, if the variable is followed by more text, as the cursor has been positioned at the start of the line (the carriage return), this text is echoed over the previous echoed data.

如何解决?

@echo off
setlocal enabledelayedexpansion

for /f "tokens=1,* delims==" %%a in ('wmic baseboard get /format:list') DO ( 


    if ["%%a"] EQU ["Product"] (
        for /f "delims=" %%c in ("%%b") do set "PlatformInfo=%%c"

        if defined PlatformInfo (
            echo(!PlatformInfo!
            echo(!PlatformInfo!This does not overwrite the variable
        )
    )

    if ["%%a"] EQU ["Version"] (
        for /f "delims=" %%c in ("%%b") do set "BaseboardVersion=%%c"

        if defined BaseboardVersion (
            echo(!BaseboardVersion!
            echo(!BaseboardVersion!This does not overwrite the variable
        )
    )   
)

在这种情况下,无需更改代码逻辑,可以使用附加的for /f命令删除附加的回车符

In this case, without changing your code logic, an aditional for /f command can be used to remove the aditional carriage return

这篇关于解析输出时如何更正变量覆盖行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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