这不是一个编程问题。是否有命令行或Windows工具(Windows 7)可以获取文本文件的当前编码?我肯定可以写一个小的C#应用程序,但我想知道是否已经内置了一些工具?
这不是一个编程问题。是否有命令行或Windows工具(Windows 7)可以获取文本文件的当前编码?我肯定可以写一个小的C#应用程序,但我想知道是否已经内置了一些工具?
附有新版记事本和Notepad++的截图。
记事本(Windows 10和11):
右下角:
"另存为..."对话框:
如果您的Windows机器上有"git"或"Cygwin",请转到文件所在的文件夹并执行以下命令:
file *
这将给您该文件夹中所有文件的编码详细信息。
file --mime-encoding
,以获取文件的编码。 - smac89Linux命令行工具“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; 目录 Debug; 目录 duration.h; ASCII C++程序文本,带有CRLF换行符 ipch; 目录 main.cpp; ASCII C程序文本,带有CRLF换行符 Precision.txt; ASCII文本,带有CRLF换行符 Release; 目录 Speed.txt; ASCII文本,带有CRLF换行符 SquareRoot.sdf; 数据 SquareRoot.sln; UTF-8 Unicode(带BOM)文本,带有CRLF换行符 SquareRoot.sln.docstates.suo; PCX ver. 2.5图像数据 SquareRoot.suo; CDF V2文档,损坏:无法读取摘要信息 SquareRoot.vcproj; XML文档文本 SquareRoot.vcxproj; XML文档文本 SquareRoot.vcxproj.filters; XML文档文本 SquareRoot.vcxproj.user; XML文档文本 squarerootmethods.h; ASCII C程序文本,带有CRLF换行符 UpgradeLog.XML; XML文档文本
C:\Users\SH\Downloads\SquareRoot>file --mime-encoding * _UpgradeReport_Files; 二进制 Debug; 二进制 duration.h; us-ascii ipch; 二进制 main.cpp; us-ascii Precision.txt; us-ascii Release; 二进制 Speed.txt; us-ascii SquareRoot.sdf; 二进制 SquareRoot.sln; utf-8 SquareRoot.sln.docstates.suo; 二进制 SquareRoot.suo; CDF V2文档,损坏:无法读取摘要信息二进制
SquareRoot.vcproj; us-ascii => SquareRoot.vcproj; 美国标准信息交换码 SquareRoot.vcxproj; utf-8 => SquareRoot.vcxproj; 通用字符集转换格式 SquareRoot.vcxproj.filters; utf-8 => SquareRoot.vcxproj.filters; 通用字符集转换格式 SquareRoot.vcxproj.user; utf-8 => SquareRoot.vcxproj.user; 通用字符集转换格式 squarerootmethods.h; us-ascii => squarerootmethods.h; 美国标准信息交换码 UpgradeLog.XML; us-ascii => UpgradeLog.XML; 美国标准信息交换码
安装 Git(在 Windows 上需要使用 Git Bash 控制台)。输入:
file --mime-encoding *
对于当前目录中的所有文件,或者
file --mime-encoding */*
对所有子目录中的文件进行操作
file --mime-encoding */*
将会忽略 */*/*
中的所有文件。
因此,如果你想要捕获整个子目录树中的 所有 文件,你还需要运行 file --mime-encoding */*/*
,以此类推。 - Henke我发现另一个有用的工具: https://archive.codeplex.com/?p=encodingchecker 可以在这里找到EXE文件 here
以下是我对如何通过BOM检测Unicode文本编码的看法。由于此方法仅适用于文本文件(特别是Unicode文件),并且在没有BOM的情况下默认为ascii
,因此此方法的准确性较低(就像大多数文本编辑器一样,在没有BOM的情况下默认为UTF8
以匹配HTTP/web生态系统)。
更新2018年:我不再推荐使用这种方法。 我建议使用GIT的file.exe或*nix工具,如@Sybren所推荐,并在后续答案中展示了如何通过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,这破坏了Windows下的GIT auto-cr-lf,默认情况下使用多年)
Get-FileEncoding
。我甚至还从 Python 和 Nodejs 中审查了 punycode,但是这个小版本对我的使用命中了 80/20(更像是 99/1)。如果您托管其他人的文件,我建议您使用 Syben 的答案(https://dev59.com/6nA65IYBdhLWcg3wogOe#34766140)中的 file
命令或另一个生产质量的 Unicode 解码器。 - yzorgfile.exe
。我还在回复其他评论中提到了这一点。我遵循这个建议,在稍后的答案(也是我的)https://dev59.com/6nA65IYBdhLWcg3wogOe#46816054中提供了有关如何从powershell使用`file.exe`的提示。您是否希望我在答案正文中包含该限定词? - yzorg一个简单的解决方案可能是在Firefox中打开文件。
文本编码将出现在“页面信息”窗口中。
注意:如果文件不是txt格式,只需将其重命名为txt并重试。
P.S. 有关更多信息,请参见this文章。
我写了第四个答案(在写作时)。但是最近我在所有的电脑上都安装了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还是我从powershell使用C:\WINDOWS\system32\where.exe
和许多其他默认情况下被powershell隐藏的操作系统CLI命令的方法, *耸肩*。
file
作为文件file.exe
的别名,而不是使用file.exe
¯\(ツ)/¯ - scrthqdir | where Size -lt 10000
和 where.exe git
。 - yzorgwhere.exe
来区分它和 PS 中的 where
,后者是 Where-Object
的内置别名。例如:
where.exe git*
与
ls . | where Size -lt 10000
- yzorgfile.exe
与 .NET 静态类,例如:[File]::SetCreationTime("readme.md", [DateTime]::Now)
。 - yzorg你可以通过在文件位置打开git bash并运行命令file -i 文件名
来简单检查它。
举个例子:
user filesData
$ file -i data.csv
data.csv: text/csv; charset=utf-8
下面是一些C代码,用于可靠地检测ASCII、BOM和UTF8编码:https://unicodebook.readthedocs.io/guess_encoding.html
只有ASCII、UTF-8和使用BOM的编码(带BOM的UTF-7、带BOM的UTF-8、UTF-16和UTF-32)有可靠的算法来获取文档的编码。对于所有其他编码,您必须依靠基于统计学的启发式方法。
编辑:
这是从Effective way to find any file's Encoding中转换为PowerShell版本的C#答案。只能使用文件签名(BOM)工作。
# 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
ASCII,UTF-8,UTF-8,UTF-8 BOM
不起作用,但似乎对UTF-16,UTF-16 BE
有效... - undefined