The*_*Guy 178 windows encoding
这不是一个真正的编程问题,是否有命令行或Windows工具(Windows 7)来获取文本文件的当前编码?当然我可以写一个小C#应用程序,但我想知道是否有内置的东西?
Mik*_*Vee 197
使用Windows附带的常规旧香草记事本打开您的文件.
单击" 另存为... " 时,它将显示文件的编码.
它看起来像这样:

无论默认选择的编码是什么,这都是您当前编码的文件.
如果是UTF-8,您可以将其更改为ANSI并单击"保存"以更改编码(反之亦然).
我意识到有许多不同类型的编码,但当我被告知我们的导出文件是UTF-8并且它们需要ANSI时,这就是我所需要的.这是一次性出口,所以记事本适合我.
仅供参考:根据我的理解,我认为" Unicode "(如记事本中所列)是UTF-16的误称.
更多关于记事本的" Unicode "选项:Windows 7  -  UTF-8和Unicdoe
小智 55
通过GnuWin32在Windows上提供(Linux)命令行工具'文件':
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
        Geo*_*nan 51
如果您的Windows机器上有"git"或"Cygwin",请转到文件所在的文件夹并执行命令:
file *
这将为您提供该文件夹中所有文件的编码详细信息.
use*_*954 23
我觉得有用的另一个工具:https://archive.codeplex.com/? p = encodingchecker EXE可以在这里找到
yzo*_*org 17
这是我如何通过BOM检测Unicode系列文本编码.此方法的准确性很低,因为此方法仅适用于文本文件(特别是Unicode文件),默认ascii情况下不存在BOM(与大多数文本编辑器一样,默认情况下,UTF8如果要匹配HTTP/Web生态系统) ).
更新2018:我不再推荐这种方法. 我建议使用@Sybren推荐的GIT或*nix工具中的file.exe,我将在后面的回答中展示如何通过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中默认为UTF-16,这打破了GIT自动CR-LF适用于Windows,这是多年的缺省值.)
phd*_*der 12
安装 git(在 Windows 上你必须使用 git bash 控制台)。类型:
file *   
对于当前目录中的所有文件,或
file */*   
对于所有子目录中的文件
Jus*_*dow 11
一个简单的解决方案可能是在 Firefox 中打开文件。
并且文本编码将出现在“页面信息”窗口中。
注意: 如果文件不是txt格式,只需将其重命名为txt并重试。
PS欲了解更多信息,请参阅这篇文章。
我写了#4答案(在撰写本文时)。但是最近我在所有计算机上都安装了git,所以现在我使用@Sybren的解决方案。这是一个新的答案,可以使该解决方案从powershell方便使用(无需将所有git / usr / bin都放在PATH中,这对我来说太麻烦了)。
将此添加到您的profile.ps1:
$global:gitbin = 'C:\Program Files\Git\usr\bin'
Set-Alias file.exe $gitbin\file.exe
而使用这样的:file.exe --mime-encoding *。您必须在命令中包含.exe,PS别名才能起作用。  
但是,如果您不自定义PowerShell profile.ps1,建议您从我的开始:https : //gist.github.com/yzorg/8215221/8e38fd722a3dfc526bbe4668d1f3b08eb7c08be0 
并将其保存到~\Documents\WindowsPowerShell。在没有git的计算机上使用是安全的,但是在找不到git时会写警告。
命令中的.exe也是我C:\WINDOWS\system32\where.exe在Powershell中使用的方式;以及其他许多Powershell * shrug *默认隐藏的OS CLI命令。
| 归档时间: | 
 | 
| 查看次数: | 288377 次 | 
| 最近记录: |