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


当前回答

安装git(在Windows上你必须使用git bash控制台)。类型:

file --mime-encoding *   

对于当前目录中的所有文件,或

file --mime-encoding */*   

对于所有子目录中的文件

其他回答

我发现做到这一点的唯一方法是VIM或notepad++。

以下是我对如何通过BOM检测Unicode文本编码家族的看法。这种方法的准确性很低,因为这种方法只适用于文本文件(特别是Unicode文件),并且在没有BOM时默认为ascii(像大多数文本编辑器一样,如果你想匹配HTTP/web生态系统,默认将是UTF8)。

2018年更新:我不再推荐这种方法。我建议使用GIT中的file.exe或@Sybren推荐的*nix工具,我将在后面的回答中展示如何通过PowerShell来实现这一点。

# from https://gist.github.com/zommarin/1480974
function Get-FileEncoding($Path) {
    $bytes = [byte[]](Get-Content $Path -Encoding byte -ReadCount 4 -TotalCount 4)

    if(!$bytes) { return 'utf8' }

    switch -regex ('{0:x2}{1:x2}{2:x2}{3:x2}' -f $bytes[0],$bytes[1],$bytes[2],$bytes[3]) {
        '^efbbbf'   { return 'utf8' }
        '^2b2f76'   { return 'utf7' }
        '^fffe'     { return 'unicode' }
        '^feff'     { return 'bigendianunicode' }
        '^0000feff' { return 'utf32' }
        default     { return 'ascii' }
    }
}

dir ~\Documents\WindowsPowershell -File | 
    select Name,@{Name='Encoding';Expression={Get-FileEncoding $_.FullName}} | 
    ft -AutoSize

建议:如果dir、ls或Get-ChildItem只检查已知的文本文件,并且只从已知的工具列表中寻找“糟糕的编码”,那么这个方法可以很好地工作。(例如SQL Management Studio默认为UTF16,这破坏了GIT auto-cr-lf for Windows,这是多年来的默认。)

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

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

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

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

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

我发现另一个有用的工具是:https://archive.codeplex.com/?p=encodingchecker EXE可以在这里找到

(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