批次:解析输出时,如何正确的变量覆盖行为不检 [英] Batch: How to correct variable overwriting misbehavior when parsing output

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

问题描述

在一个批处理文件,我检查主板信息,具有下列内容:结果
BaseboardCheck.cmd

 关闭@echo
SETLOCAL enabledelayedexpansionFOR / F令牌= 1,2 * delims ==%%一个在('WMIC基板获得/格式:名单)DO(    如果[%%一个] EQU [产品(
        设置PlatformInfo = %% b        如果定义PlatformInfo(
            回音!PlatformInfo!
            回音!PlatformInfo!这将覆盖变量
        )
    )    如果[%%一个] EQU [版本(
        设置BaseboardVersion = %% b        如果定义BaseboardVersion(
            回音!BaseboardVersion!
            回音!BaseboardVersion!这将覆盖变量
        )
    )

上面的问题:变量被覆盖,而不是附加到,当回显的出结果
输出:

  DX79SI
这将覆盖变量
AAG28808-600
这将覆盖变量

我想获得的是:

  DX79SI
DX79SIThis覆盖变量
AAG28808-600
AAG28808-600This覆盖变量

我花了几个小时,在这个(并将继续这样做),但我希望其他人遇到了这个问题。 ,我希望其他人谁运行到这个问题的解析能够避免在将来。搜索结果

这出来这样做的另一个问题是,它似乎打破条件逻辑。

更新结果
所有的援助后,我想出了这个解决方案:结果

  FOR / F跳过= 2令牌= 1,2 = delims,%%一个在('WMIC踢脚线获得产品^,^版本,宽度/格式:CSV)做(    设置平台= %%一
    设置BaseboardVersion = %% b

echo.Platform:%平台%
echo.Version%BaseboardVersion%。


解决方案

是的,你有问题,但它不是你的想法。

WMIC 具有特定行为:在每行输出的尽头有一个aditional的回车,也就是每一行的符进行结束符进行的0x0A

这aditional的回车保存您的变量内,回荡安慰的时候,你得到的数据并回车,因此,如果变量后跟更多的文字,当光标已经位于的开始行(回车),该文本回荡在previous回波数据。

如何解决?

 关闭@echo
SETLOCAL enabledelayedexpansionFOR / F令牌= 1,* delims ==%%一个在('WMIC基板获得/格式:名单)DO(
    如果[%%一个] EQU [产品(
        FOR / Fdelims =%%℃的(%% B)并设置PlatformInfo = %% C        如果定义PlatformInfo(
            回声(!PlatformInfo!
            回声(!PlatformInfo!这不会覆盖变量
        )
    )    如果[%%一个] EQU [版本(
        FOR / Fdelims =%%℃的(%% B)并设置BaseboardVersion = %% C        如果定义BaseboardVersion(
            回声(!BaseboardVersion!
            回声(!BaseboardVersion!这不会覆盖变量
        )
    )

在这种情况下,不改变你的code的逻辑,一个aditional的 FOR / F 命令可用于去除aditional的回车

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

What I'd like to get to is:

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 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.

How to solve?

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

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天全站免登陆