How to correct variable overwriting misbehavior when parsing output?

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

First, I could not believe what happens on batch file execution.

I executed on Windows XP SP3 x86 after many trials the command line

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

and looked on file Output.txt with viewer of file manager Total Commander.
I saw two empty lines at top, but that does not really matter. So I continued with other trials.

Later I opened Output.txt in text editor UltraEdit and saw immediately on status bar U-DOS indicating that the output file is encoded in UTF-16 Little Endian having DOS line terminators. I switched to hex edit mode and could see:

00000000h: FF FE 0D 00 0A 00 0D 00 0A 00 43 00 61 00 70 00 ; ÿþ........C.a.p.
00000010h: 74 00 69 00 6F 00 6E 00 3D 00                   ; t.i.o.n.=.

So the output file is indeed a Unicode file encoded with UTF-16 LE with BOM (byte order mark). There is no CR CR LF. All line terminations are correct the CR LF pair (carriage return + line-feed).

Now I searched on Stack Overflow for questions with [batch-file] containing the words wmic Unicode and found cmd is somehow writing Chinese text as output.

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

Better is the answer by Dharma Leonardi as the solution with using command type results in a correct conversion of the Unicode output to ANSI output as long as the output does not contain characters not available in ANSI code page.

Note: The term ANSI is used here by me not really 100% precise for single byte per character encoding. See the Wikipedia articles about character encoding, code page and Windows code page. In a Windows command processor console is used by default an OEM code page like 437 (North American countries) or 850 (Western European countries). See also Why are Danish characters not displayed as in text editor on executing batch file? There is explained on an example how the same small Danish text can be encoded with which bytes using various character encodings used usually in North American and Western European countries.

However, after changing the batch code to process output of wmic.exe with ANSI encoding, the line with if defined BaseboardVersion evaluated always to true although I could not see that variable BaseboardVersion contained any data and therefore next line resulted in showing echo status.

It took me again some time to find out with inserting set > Variables.txt above the condition and looking on this file that on my computer the version string is just a single space character. The value of Version was the only value of all keys without a string right of the equal sign consisting only of a single space.

Here is the batch file which finally worked on my computer and which produces the expected output:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
%SystemRoot%\System32\wbem\wmic.exe /OUTPUT:"%TEMP%\UnicodeData.tmp" baseboard get /format:list
for /f "tokens=1,2* delims==" %%I in ('type "%TEMP%\UnicodeData.tmp"') do (
    if "%%I" == "Product" (
        if not "%%J" == "" if not "%%J" == " " (
            set "PlatformInfo=%%J"
            echo !PlatformInfo!
            echo !PlatformInfo!This overwrites the variable
        )
    ) else if "%%I" == "Version" (
        if not "%%J" == "" if not "%%J" == " " (
            set "BaseboardVersion=%%J"
            echo !BaseboardVersion!
            echo !BaseboardVersion!This overwrites the variable
        )
    )
)
del "%TEMP%\UnicodeData.tmp"
endlocal

Leave a Comment