2010-09-14 60 views
98

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

+8

** _对Windows中文件的猜测编码是标题应该是什么。如果你事先不知道,你永远无法猜测。 – 2016-03-11 00:57:05

回答

114

使用Windows自带的普通旧式香草记事本打开您的文件。
当您单击“另存为...”时,它会显示文件的编码。
它会是这样的: enter image description here

无论默认选择的编码,也就是你当前的编码是什么文件。
如果是UTF-8,则可以将其更改为ANSI,然后单击保存更改编码(反之亦然)。

我意识到存在许多不同类型的编码,但是当我被告知我们的导出文件是UTF-8并且他们需要ANSI时,这就是我所需要的。这是一次性出口,所以记事本适合我的账单。

仅供参考:从我的理解中我认为“Unicode”“(如记事本中列出的)对于UTF-16是不恰当的。
这里更多关于记事本的 “统一” 选项:Windows 7 - UTF-8 and Unicdoe

+0

在Windows 8和编辑器(这似乎是替代品)上没有记事本没有此功能 – Alex 2015-07-20 12:28:41

+1

@Alex,我不使用Win-8。执行谷歌搜索,我发现这个链接:[Win-8记事本](https://echlinm.wordpress.com/2011/11/06/windows-8-start-menu-where-did-notepad-go/) 。我希望你能找到它,因为我向你保证,它仍然存在。 – MikeTeeVee 2015-07-20 15:48:28

+1

谢谢,但在Windows 8.1上肯定没有应用程序称为记事本。当您在搜索中输入记事本时,会出现“编辑器”。并且这没有endoding下拉菜单,并且没有菜单 – Alex 2015-07-20 19:50:32

18

另一种工具,我发现有用:http://encodingchecker.codeplex.com/

+2

真正有助于分析多个文件 – 2014-03-11 14:20:50

+1

即使对于非常大的文件,也能即时回答(如人们所期望的)。 – 2016-08-04 17:24:47

+1

似乎无法在Windows 10上运行。 – plasmacel 2016-10-05 01:06:59

17

使用

# 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 
+0

'Get-FileEncoding'似乎不存在于我的系统上。它是一个自定义cmdlet吗? – 2015-11-23 09:11:36

+1

http://poshcode.org/2059 – 2017-02-27 06:02:21

+0

poshcode上有许多* Get-FileEncoding变体。我甚至评论过python和nodejs中的punycode,但是这个小版本在我的使用中达到了80/20(更像99/1)。如果您托管其他人的文件,我建议您使用Syben的答案(https://stackoverflow.com/a/34766140/195755)或其他生产质量的unicode解码器中的'file'命令。 – yzorg 2017-05-27 17:28:30

27

的(Linux)的命令行工具'我拿文件'可通过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 
+0

请注意,您可能需要git 2.x,我没有git 1.9.5 – 2016-08-31 14:47:45

+0

对于我的文件,它说“二进制”:( – 2017-08-23 12:45:51

+0

令人难以置信的是不得不恢复到基本操作的命令行,这是2017,但它看起来没问题 – 2017-09-18 16:47:28

2

类似于上文记事本列出的解决方案,你也可以打开Visual Studio中的文件,如果您使用的。在Visual Studio中,您可以选择“文件>高级保存选项...”

“编码:”组合框会告诉您具体哪个编码当前正在用于该文件。它有比记事本更多的文本编码,所以在处理来自世界各地的文件和其他文件时很有用。

就像记事本一样,您也可以从选项列表中更改编码,然后点击“确定”后保存文件。您也可以通过另存为对话框中的“使用编码保存...”选项选择所需的编码(通过单击保存按钮旁边的箭头)。

+0

不错,但是当我尝试用Visual Studio打开文件时,它总是在关联的文本编辑器(记事本+ +这种文件扩展名)中打开文件。 – 2017-08-23 12:45:21

+0

@ barbara.post在Visual Studio中,我可以访问任何类型的纯文本文件,你可能会告诉它只要记得遇到那是我的想法,至少。 – JaykeBird 2017-09-12 08:42:43

14

如果你有“混帐”或在Windows机器上“的Cygwin”,然后去到你的文件存在的文件夹,并执行以下命令:

file * 

这会给你所有的编码细节该文件夹中的文件。

+1

希望我能给更多的选票!对于https://stackoverflow.com/q/6855712/2946116 – patricktokeeffe 2017-12-22 01:29:29

+0

非常有价值请加入avengers – Nitin 2018-02-12 23:47:21

0

我发现这样做的唯一方法是VIM或Notepad ++。

1

我写了#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 *。您的必须在用于PS别名的命令中包含.exe

但是,如果您不定制您的PowerShell profile.ps1,我建议您从我的开始:https://gist.github.com/yzorg/8215221/8e38fd722a3dfc526bbe4668d1f3b08eb7c08be0 并将其保存到~\Documents\WindowsPowerShell。在没有git的计算机上使用是安全的,但是当找不到git时会写警告。

.exe在命令中也是如何使用powershell中的C:\WINDOWS\system32\where.exe;以及powershell * shrug *默认隐藏的许多其他操作系统CLI命令。

相关问题