检查文件是否未两次编码

时间:2018-09-12 14:22:26

标签: powershell character-encoding

我用这个问题的答案: Using PowerShell to write a file in UTF-8 without the BOM

将文件(UCS-2)编码为UTF-8。问题是,如果我两次(或多次)运行编码,则Cyrillic文本会损坏。如果文件已经在UTF-8中,如何停止编码?

代码是:

$MyFile = Get-Content $MyPath
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($MyPath, $MyFile, $Utf8NoBomEncoding)

1 个答案:

答案 0 :(得分:1)

使用:

$MyFile = Get-Content -Encoding UTF8 $MyPath
  • 最初,当$MyPath被UTF-16LE编码(“ Unicode”编码,我假设这是您的意思)时,PowerShell将忽略 -Encoding参数,因为文件中存在 BOM ,可以明确标识编码。

    • 如果您的原始文件 没有BOM,则需要做更多的工作。
  • $MyPath保存为UTF-8 且没有BOM 后,您必须告诉Windows PowerShell [1] ,您期望使用-Encoding UTF8进行UTF-8编码,因为它默认情况下会将文件解释为“ ANSI”编码(根据与遗留系统语言环境相关联的典型单字节代码页进行编码)。


[1]请注意,cross-platform PowerShell Core edition默认为无BOM的UTF-8。