Search code examples
batch-fileencodingcmdwmicipconfig

Issue with batch file encoding


I have a batch file script I am trying to create to obtain some pretty basic info on the computers at my job site. However, I am having severe issues with encoding. WMIC and IPCONFIG put out different encodings, and as such, it is causing me to obtain garbled results when the commands execute. Thus far, I have:

@echo off

if exist "C:\%computername%-info.txt" (
    echo Your computer information is already published! Skipping
) else (

    ipconfig /all > "C:\%computername%-info.txt" 
    wmic bios get serialnumber >> "C:\%computername%-info.txt"
)

pause

The results are...weird. I don't know what the encoded output is for each of these tools, and I still have more to add...does anyone know a fix? I don't know too much about codepages (chcp) but I've tried several and still get the same garbled results.


Solution

  • Ipconfig output is ANSI, wmic is unicode

    If you output wmic before ipconfig, when readed it is considered unicode, as the start or the file is it. And output of ipconfig is garbage as there is no double byte characters.

    If ipconfig is used before wmic, file is considered ansi and output of wmic is space separated

    Use find to filter and reencode to ansi

    wmic bios get serialnumber | find /v "" >> "C:\%computername%-info.txt"
    

    So all command output is ansi