这不是一个真正的编程问题,是否有命令行或Windows工具(Windows 7)来获取文本文件的当前编码?当然,我可以写一个小c#应用程序,但我想知道是否有一些已经内置?


当前回答

一个简单的解决方案可能是在Firefox中打开该文件。

将文件拖放到firefox中 按Ctrl+I打开页面信息

和文本编码将出现在“页面信息”窗口。

注意:如果文件不是txt格式,请将其重命名为txt,然后重试。

附:欲了解更多信息,请参阅这篇文章。

其他回答

类似于上面用记事本列出的解决方案,如果你正在使用Visual Studio,你也可以在Visual Studio中打开文件。在Visual Studio中,您可以选择“File > Advanced Save Options…”

“Encoding:”组合框将明确地告诉您当前文件使用的是哪种编码。它比记事本列出了更多的文本编码,所以它在处理来自世界各地的各种文件时很有用。

就像记事本一样,你也可以从选项列表中更改编码,然后在点击“确定”后保存文件。你也可以通过Save As对话框中的“Save with encoding…”选项来选择你想要的编码(通过单击Save按钮旁边的箭头)。

您可以在文件位置上打开git bash,然后运行命令file -i file_name来检查

例子

user filesData
$ file -i data.csv
data.csv: text/csv; charset=utf-8

这里有一些可靠的ascii、bom和utf8检测的C代码:https://unicodebook.readthedocs.io/guess_encoding.html

仅ASCII, UTF-8和编码使用BOM (UTF-7与BOM, UTF-8与BOM, UTF-16和UTF-32)有可靠的算法来获取文档的编码。 对于所有其他编码,您必须信任基于统计的启发式。

编辑:

一个powershell版本的c#答案来自:找到任何文件编码的有效方法。只适用于签名(炸弹)。

# get-encoding.ps1
param([Parameter(ValueFromPipeline=$True)] $filename)    
begin {
  # set .net current directoy                                                                                                   
  [Environment]::CurrentDirectory = (pwd).path
}
process {
  $reader = [System.IO.StreamReader]::new($filename, 
    [System.Text.Encoding]::default,$true)
  $peek = $reader.Peek()
  $encoding = $reader.currentencoding
  $reader.close()
  [pscustomobject]@{Name=split-path $filename -leaf
                BodyName=$encoding.BodyName
                EncodingName=$encoding.EncodingName}
}


.\get-encoding chinese8.txt

Name         BodyName EncodingName
----         -------- ------------
chinese8.txt utf-8    Unicode (UTF-8)


get-childitem -file | .\get-encoding

寻找一个Node.js/npm解决方案?试试encoding-checker:

npm install -g encoding-checker

使用

Usage: encoding-checker [-p pattern] [-i encoding] [-v]
 
Options:
  --help                 Show help                                     [boolean]
  --version              Show version number                           [boolean]
  --pattern, -p, -d                                               [default: "*"]
  --ignore-encoding, -i                                            [default: ""]
  --verbose, -v                                                 [default: false]

例子

获取当前目录下所有文件的编码:

encoding-checker

返回当前目录下所有md文件的编码:

encoding-checker -p "*.md"

获取当前目录及其子文件夹中所有文件的编码(对于巨大的文件夹将需要相当长的时间;看似无响应):

encoding-checker -p "**"

更多示例请参考npm文档或官方存储库。

(Linux)命令行工具'file'可通过GnuWin32在Windows上使用:

http://gnuwin32.sourceforge.net/packages/file.htm

如果你安装了git,它位于C:\Program Files\git\usr\bin.

例子:

    C:\Users\SH\Downloads\SquareRoot>file *
    _UpgradeReport_Files;         directory
    Debug;                        directory
    duration.h;                   ASCII C++ program text, with CRLF line terminators
    ipch;                         directory
    main.cpp;                     ASCII C program text, with CRLF line terminators
    Precision.txt;                ASCII text, with CRLF line terminators
    Release;                      directory
    Speed.txt;                    ASCII text, with CRLF line terminators
    SquareRoot.sdf;               data
    SquareRoot.sln;               UTF-8 Unicode (with BOM) text, with CRLF line terminators
    SquareRoot.sln.docstates.suo; PCX ver. 2.5 image data
    SquareRoot.suo;               CDF V2 Document, corrupt: Cannot read summary info
    SquareRoot.vcproj;            XML  document text
    SquareRoot.vcxproj;           XML document text
    SquareRoot.vcxproj.filters;   XML document text
    SquareRoot.vcxproj.user;      XML document text
    squarerootmethods.h;          ASCII C program text, with CRLF line terminators
    UpgradeLog.XML;               XML  document text

    C:\Users\SH\Downloads\SquareRoot>file --mime-encoding *
    _UpgradeReport_Files;         binary
    Debug;                        binary
    duration.h;                   us-ascii
    ipch;                         binary
    main.cpp;                     us-ascii
    Precision.txt;                us-ascii
    Release;                      binary
    Speed.txt;                    us-ascii
    SquareRoot.sdf;               binary
    SquareRoot.sln;               utf-8
    SquareRoot.sln.docstates.suo; binary
    SquareRoot.suo;               CDF V2 Document, corrupt: Cannot read summary infobinary
    SquareRoot.vcproj;            us-ascii
    SquareRoot.vcxproj;           utf-8
    SquareRoot.vcxproj.filters;   utf-8
    SquareRoot.vcxproj.user;      utf-8
    squarerootmethods.h;          us-ascii
    UpgradeLog.XML;               us-ascii