你知道有什么方法可以通过编程或通过脚本将一组以ansi字符编码保存的文本文件转换为unicode编码吗?
我想执行的操作与使用记事本打开文件并选择将其另存为unicode文件时执行的操作相同。
发布于 2009-02-06 13:54:14
您可以使用iconv。在Windows上,你可以在Cygwin下使用它。
iconv -f from_encoding -t to_encoding file发布于 2009-02-07 00:13:21
这可能适用于您,但请注意,它将抓取当前文件夹中的每个文件:
Get-ChildItem | Foreach-Object { $c = (Get-Content $_); `
Set-Content -Encoding UTF8 $c -Path ($_.name + "u") }为了简洁起见,使用别名也是如此:
gci | %{ $c = (gc $_); sc -Encoding UTF8 $c -Path ($_.name + "u") }Steven Murawski建议改用Out-File。这两个cmdlet之间的差异如下:
Out-File将尝试格式化receives.Out-File's默认编码是基于Unicode的输入,而Set-Content使用系统的默认编码。下面是一个示例,假设文件test.txt在这两种情况下都不存在:
PS> [system.string] | Out-File test.txt
PS> Get-Content test.txt
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
# test.txt encoding is Unicode-based with BOMPS> [system.string] | Set-Content test.txt
PS> Get-Content test.txt
System.String
# test.txt encoding is "ANSI" (Windows character set)实际上,如果您不需要任何特定的Unicode编码,您也可以执行以下操作将文本文件转换为Unicode:
PS> Get-Content sourceASCII.txt > targetUnicode.txtOut-File是一种“带可选参数的重定向操作符”。
发布于 2009-02-06 14:19:25
最简单的方法是Get-Content 'path/to/text/file‘| out-file 'name/of/file’。
Out-File has an -encoding parameter,默认为Unicode。
如果你想编写一批脚本,你可以这样做
$files = get-childitem 'directory/of/text/files'
foreach ($file in $files)
{
get-content $file | out-file $file.fullname
}https://stackoverflow.com/questions/520150
复制相似问题