Search code examples
windowsbatch-filefor-loopcmdwmic

How to correct variable overwriting misbehavior when parsing output?


I am checking for baseboard information in a batch file with the following code.

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 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 I am hoping someone else has run into this issue. And I 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%

Solution

  • 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