Package: how to fix rewriting variable of incorrect behavior when analyzing output

In the batch file, I check the 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: variables are overwritten, not added to echo'd. Exit:

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

I would like to:

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

I spent a few hours on this (and will continue to do this), but I hope someone else comes across this problem. And I hope that someone who comes across this parsing problem can avoid this in the future.

An additional problem arising from this is that it seems to violate conditional logic.

Update
After all the help, 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%. 
+2
source share
3 answers

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

wmic has a specific behavior: at the end of each line output there is a secondary carriage return, that is, each line ends with 0x0d 0x0d 0x0a

This secondary carriage return is stored inside your variable, and when the echo to the console, you get data and carriage return, so if the variable is followed by more text, because the cursor was placed at the beginning of line (carriage return), this text is reflected by the previous ones echo 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 the logic of the code, you can use the additional for /f command to remove the returned carriage return

+3
source

Wow, it was really hard to find out what was going on here.

Firstly, I couldnโ€™t believe what happens when the batch file is executed.

After many tests performed on Windows XP SP3 x86

 wmic.exe baseboard get /format:list > Output.txt 

and looked at the Output.txt file by viewing the Total Commander file manager. I saw two empty lines at the top, but that doesn't really matter. Therefore, I continued other tests.

Later, I opened Output.txt in an UltraEdit text editor and immediately saw the U-DOS status bar, indicating that the output file was encoded in small UTF-16 content with DOS line terminators. I switched to hex editing mode and could see

 00000000h: FF FE 0D 00 0A 00 0D 00 0A 00 43 00 61 00 70 00 ; รฟรพ........Cap 00000010h: 74 00 69 00 6F 00 6E 00 3D 00 ; tion=. 

So the output file is really a Unicode file with UTF-16 LE BOM. CR CR LF is absent. All lines complete the correct CR LF pair (carriage return + line).

Now I searched in Stack for questions with a batch file containing the words wmic Unicode and found cmd somehow writing Chinese text as a result .

The accepted dbenham answer is not very good as it creates an ANSI version of the Unicode output for wmic.exe , but the ANSI file now really contains 0D 0D 0A (= CR CR LF).

It is better to answer Dharma Leonardi , since a solution using the type command will correctly convert the Unicode output to ANSI output, until the output contains characters that are not available on the ANSI code page.

But after changing the batch code to process the ANSI-encoded wmic.exe output, the line with if defined BaseboardVersion always evaluated as true, although I could not see that this BaseboardVersion variable contained any data, and therefore the next line led to the display of the echo state.

It took me a while to learn how to insert set > Variables.txt over this condition and look at this file, that on my computer the version line is just one space. The Version value was the only value of all keys without an equal sign on the right, consisting of only one space.

Here is a batch file that finally worked on my computer and produces the expected result:

 @echo off setlocal enabledelayedexpansion wmic.exe /OUTPUT:"%TEMP%\UnicodeData.tmp" baseboard get /format:list for /f "usebackq tokens=1,2* delims==" %%a in (`type "%TEMP%\UnicodeData.tmp"`) do ( if "%%a" == "Product" ( if not "%%b" == "" if not "%%b" == " " ( set "PlatformInfo=%%b" echo !PlatformInfo! echo !PlatformInfo!This overwrites the variable ) ) else if "%%a" == "Version" ( if not "%%b" == "" if not "%%b" == " " ( set "BaseboardVersion=%%b" echo !BaseboardVersion! echo !BaseboardVersion!This overwrites the variable ) ) ) del "%TEMP%\UnicodeData.tmp" endlocal 
+4
source

As already mentioned, the problem is with the wmic line ending.

You can overcome this problem if you do not use the end of the line: wmic baseboard get Product,Version,Width uses three tokens: Product, Version and Width (in most cases it is empy). So Output will be: DX79SI,AAG28808-600, We use token 1 and token 2, ignoring token 3 (this would have a problem)

 set Platform=undefined set BaseboardVersion=undefined for /f "tokens=1,2 delims=," %%a in ('wmic baseboard get Product^,Version^,Width^|findstr "."') do ( set Platform=%%a set BaseboardVersion=%%b ) echo it is a %Platform% with Version %BaseboardVersion%. No overwriting 

I also added delims=, in case any of the lines contains a space (unlikely with product )

+1
source

All Articles