批处理文件编码问题 [英] Issue with batch file encoding

查看:122
本文介绍了批处理文件编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个批处理文件脚本,以在我的工作站点的计算机上获取一些非常基本的信息.但是,我在编码方面遇到了严重的问题. WMIC和IPCONFIG发出了不同的编码,因此,当我执行命令时,我得到了乱码.到目前为止,我有:

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

结果很奇怪.我不知道这些工具中的每一个的编码输出是什么,而且我还需要添加更多...有人知道修复程序吗?我对代码页(chcp)不太了解,但是我尝试了几次,但仍然得到相同的乱码结果.

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.

推荐答案

ipconfig输出是ANSI,wmic是unicode

Ipconfig output is ANSI, wmic is unicode

如果在ipconfig之前输出wmic,则在读取时将其视为unicode,因为它是开始文件或文件. ipconfig的输出是垃圾,因为没有双字节字符.

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.

如果在wmic之前使用ipconfig,则将文件视为ansi,并且wmic的输出以空格分隔

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

使用find过滤并重新编码为ansi

Use find to filter and reencode to ansi

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

所以所有命令输出都是ansi

So all command output is ansi

这篇关于批处理文件编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆