从 powershell 调用时 Iconv 正在转换为 UTF-16 而不是 UTF-8
我在尝试使用 powershell 脚本中的 iconv 将某些文件的编码从 ISO-8859-1 批量转换为 UTF-8 时遇到问题。
我有这个bat文件,工作正常:
for %%f in (*.txt) do (
echo %%f
C:\"Program Files"\GnuWin32\bin\iconv.exe -f iso-8859-1 -t utf-8 %%f > %%f.UTF_8_MSDOS
)
我需要转换目录结构上的所有文件,所以我编写了另一个脚本,这次使用powershell:
Get-ChildItem -Recurse -Include *.java |
ForEach-Object {
$inFileName = $_.DirectoryName + '\' + $_.name
$outFileName = $inFileName + "_UTF_8"
Write-Host Convirtiendo $inFileName -> $outFileName
C:\"Program Files"\GnuWin32\bin\iconv.exe -f iso-8859-1 -t utf-8 $inFileName > $outFileName
}
使用这个文件的结果是将文件转换为UTF-16。我不知道我做错了什么。
有人能帮我解决这个问题吗?难道是powershell本身的编码有问题吗?
我正在使用 W7 和 WXP 和 LibIconv 1.9.2
I have a problem while trying to batch convert the encoding of some files from ISO-8859-1 to UTF-8 using iconv in a powershell script.
I have this bat file, that works ok:
for %%f in (*.txt) do (
echo %%f
C:\"Program Files"\GnuWin32\bin\iconv.exe -f iso-8859-1 -t utf-8 %%f > %%f.UTF_8_MSDOS
)
I need to convert all files on the directories structure, so I programmed this other script, this time using powershell:
Get-ChildItem -Recurse -Include *.java |
ForEach-Object {
$inFileName = $_.DirectoryName + '\' + $_.name
$outFileName = $inFileName + "_UTF_8"
Write-Host Convirtiendo $inFileName -> $outFileName
C:\"Program Files"\GnuWin32\bin\iconv.exe -f iso-8859-1 -t utf-8 $inFileName > $outFileName
}
And using this the result is the files be converted to UTF-16. I have no clue about what I am doing wrong.
Could anyone help me with this? Could be it some kind of problem with the encoding of powershell itself?
I am using W7 and WXP and LibIconv 1.9.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
>
本质上是使用默认编码为 Unicode 的 Out-File cmdlet。尝试:或使用参数:
并且由于 iconv.exe 以 UTF8 输出,因此您必须告诉 .NET 控制台子系统如何解释 stdin 流,如下所示(在 iconv.exe 之前执行此操作):
>
essentially is using the Out-File cmdlet who's default encoding is Unicode. Try:or with params:
And since iconv.exe is outputting in UTF8, you have to tell the .NET console subsystem how to intrepret the stdin stream like so (execute this before iconv.exe):