在Windows中获取文件的编码

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

  • 自Windows 3以来,记事本至少安装在所有版本的Windows中. (6认同)
  • 此方法不适用于太大而无法打开记事本的文件.而且这个限制比Notepad ++等其他编辑器要快得多.我的Windows 8.1有记事本.查看%windir%\ system32 \notepad.exe可能吗? (4认同)
  • Windows 8和Windows 10中存在记事本。 (3认同)

小智 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 *
Run Code Online (Sandbox Code Playgroud)

这将为您提供该文件夹中所有文件的编码详细信息.

  • 回答这个问题的完整命令不是盲目地运行 file 命令,而是“file --mime-encoding”来获取文件的编码 (10认同)
  • 2020年,问题不再是cygwin,而是wsl或wsl2。西格温快死了。 (3认同)
  • 到 2021 年,这可以在 git-bash(又名“Git for Windows”附带的 shell)中运行。它使用 MinGW,而不是 Cygwin。 (3认同)

use*_*954 23

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

  • 分析多个文件真的很有帮助 (4认同)
  • 在Windows 10上似乎不起作用。 (2认同)
  • 无法确定exe文件在该页面上的位置。链接是否过时? (2认同)
  • [https://github.com/amrali-eg/EncodingChecker](https://github.com/amrali-eg/EncodingChecker) 有一个修改版本。 (2认同)

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
Run Code Online (Sandbox Code Playgroud)

建议:如果这可以很好地工作dir,lsGet-ChildItem仅检查已知的文本文件,当你只希望从工具已知名单"坏编码".(即SQL Management Studio中默认为UTF-16,这打破了GIT自动CR-LF适用于Windows,这是多年的缺省值.)


phd*_*der 12

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

file *   
Run Code Online (Sandbox Code Playgroud)

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

file */*   
Run Code Online (Sandbox Code Playgroud)

对于所有子目录中的文件


Jus*_*dow 11

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

  1. 将文件拖放到 Firefox 中
  2. 在页面上右击
  3. 选择“查看页面信息”

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

在此处输入图片说明

注意: 如果文件不是txt格式,只需将其重命名为txt并重试。

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

  • 看起来“查看页面信息”不再存在,[Firefox 88 已悄悄删除了这些功能](https://winaero.com/firefox-88-has-quietly-removed-these-features/) 从 2021 年 4 月开始。CTRL -I(2022 年 1 月的 Windows 操作系统)作为解决方法 (2认同)

yzo*_*org 7

我写了#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
Run Code Online (Sandbox Code Playgroud)

而使用这样的: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命令。