2

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.

James
  • 1,653
  • 2
  • 31
  • 60

3 Answers3

4

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

MC ND
  • 69,615
  • 8
  • 84
  • 126
2

WMIC output encoding is Unicode, I don't know if Ipconfig uses the same encoding.

Try to set the output encoding of CMD to ANSI:

CMD /A /C "Your command here"

Cmd /? Help:

/A Output ANSI characters


Then try this:

CMD /A /C "ipconfig /all > "C:\%computername%-info.txt""
CMD /A /C "wmic bios get serialnumber >> "C:\%computername%-info.txt""
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Still no...and this would require me having to fire up CMD with another batch file, then call MY batch file inside that same CMD window...is there a more inline solution? I can code a C# program to do this, but would rather not... – Christopher D Albert Oct 21 '13 at 20:18
  • Are you sure that your batch-file is saved with `ANSI` encoding?, you can check it in `Notepad > File > Save as`. – ElektroStudios Oct 21 '13 at 20:20
  • Hmmm...it's still doing garbled things, regardless of the output I set it as...I tried ANSI for both commands, then changed WMIC to Unicode...still garbled. @echo off CMD /U /C wmic bios get serialnumber >> "C:\%computername%-info.txt" CMD /A /C ipconfig /all >> "C:\%computername%-info.txt" – Christopher D Albert Oct 21 '13 at 20:24
  • Can you specify what means garbled for you? really I've never seen CMD to write illegible characters, as a last resort try this: `(For /F "Delims=" %%# in ('wmic bios get serialnumber ^| find /i /v ""') Do (Echo %%#))> "C:\%computername%-info.txt"` – ElektroStudios Oct 21 '13 at 20:25
  • And in response to your question, yes, it is ANSI – Christopher D Albert Oct 21 '13 at 20:25
  • SerialNumber 2UA1111H10 ഍圊湩潤獷䤠⁐潃普杩牵瑡潩൮਍഍ †††䠠獯⁴慎敭⸠⸠⸠⸠⸠⸠⸠⸠⸠⸠⸠⸠㨠䄠䡐唲ㅁㄱ䠱〱呄഍ †††倠楲慭祲䐠獮匠晵楦⁸⸠⸠⸠⸠⸠⸠⸠㨠甠び㈰献敩敭獮渮瑥഍ †††丠摯⁥祔数⸠⸠⸠⸠⸠⸠⸠⸠⸠⸠⸠⸠㨠䠠批楲൤਍††††偉删畯楴杮䔠慮汢摥‮‮‮‮‮‮‮‮›潎഍ †††圠义⁓牐硯⁹湅扡敬⹤⸠⸠⸠⸠⸠⸠⸠㨠丠൯਍††††乄⁓畓晦硩匠慥捲⁨楌瑳‮‮‮‮‮‮›獵〰⸲楳浥湥⹳敮൴਍††††††††††††††††††††††獵〰⸲楳浥湥⹳敮൴਍††††††††††††††††††††††楳浥湥⹳敮൴਍഍䔊桴牥敮⁴摡灡整⁲潌慣牁慥䌠湯敮瑣潩㩮഍ഊ਍††††潃湮捥楴湯猭数楣楦⁣乄⁓畓晦硩†‮›獵〰⸲楳浥湥⹳敮൴਍††††敄捳楲瑰潩‮‮‮‮‮‮‮‮‮‮‮›湉整⡬⥒㠠㔲㜶䵌㌭䜠杩扡瑩丠瑥潷歲䌠湯敮瑣潩൮਍††††桐獹捩污䄠摤敲獳‮‮‮‮‮‮‮‮‮›㠷䄭ⵃぃ䄭ⴹ䍅䈭ഹ਍††††桄灣䔠慮汢摥‮‮‮‮‮‮‮‮‮‮‮›教൳਍††††畁潴潣普杩牵瑡潩湅扡敬⁤‮‮‮‮›教൳਍††††偉䄠摤敲獳‮‮‮‮‮‮‮‮‮‮‮‮›㘱⸱㌱⸴㤶ㄮ〸഍ †††匠扵敮⁴慍歳⸠⸠ – Christopher D Albert Oct 21 '13 at 20:26
  • That is what is coming as output for IPConfig. If I do IPConfig first it outputs normally, then WMIC outputs semi normally, with what looks like untranslatable unicode characters mixed in... – Christopher D Albert Oct 21 '13 at 20:27
  • For the WMIC executable try to change the default output format: `wmic bios get serialnumber /format:list` – ElektroStudios Oct 21 '13 at 20:29
  • Are you using a custom codepage in your code? type `chcp` to check the current codepage of the console. – ElektroStudios Oct 21 '13 at 20:32
  • Codepage is 437...I never changed it – Christopher D Albert Oct 21 '13 at 20:34
  • in your script file, put this coomand at the beginning: `CHCP 800` and try again to see the output – ElektroStudios Oct 21 '13 at 20:36
  • Sotty my fault, I would mean `850`. – ElektroStudios Oct 21 '13 at 20:46
0

try

wmic /append:"%computername%-info.txt" bios get serialnumber 

or for nicer output:

wmic /append:"%computername%-info.txt" bios get serialnumber /value

no garbage (but some empty lines, which the solution from MC_ND doesn't have) Nevertheless, you may add the /value to his solution

Stephan
  • 53,940
  • 10
  • 58
  • 91