问题
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.
回答1:
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
回答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""
回答3:
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
来源:https://stackoverflow.com/questions/19503794/issue-with-batch-file-encoding