PowerShellscript,错误的文件编码对话
我有一个用于文件字符编码对话的PowerShell脚本。
Get-ChildItem -Path D:/test/data -Recurse -Include *.txt |
ForEach-Object {
$inFileName = $_.DirectoryName + '\' + $_.name
$outFileName = $inFileName + "_utf_8.txt"
Write-Host "windows-1251 to utf-8: " $inFileName -> $outFileName
E:\bin\iconv\iconv.exe -f cp1251 -t utf-8 $inFileName > $outFileName
}
但它不是将 utf-8 转换为文件字符编码,而是将其转换为 utf-16。当我从命令行调用 iconv 实用程序时,它工作正常。
我有什么错吗?
I have a PowerShell script for the conversation of file character encoding.
Get-ChildItem -Path D:/test/data -Recurse -Include *.txt |
ForEach-Object {
$inFileName = $_.DirectoryName + '\' + $_.name
$outFileName = $inFileName + "_utf_8.txt"
Write-Host "windows-1251 to utf-8: " $inFileName -> $outFileName
E:\bin\iconv\iconv.exe -f cp1251 -t utf-8 $inFileName > $outFileName
}
But instead of utf-8 it converts file character encoding into utf-16. When I invoke the iconv utility from command line it works fine.
What do I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将输出重定向到文件时,Powershell 使用 Unicode 作为默认编码。您可以使用
-Encoding UTF8
开关通过管道传输到Out-File
,而不是使用重定向运算符。以下 TechNet 文章提供了更多信息(相当于 Powershell v2 中的
Get-Help Out-File -full
)。如果它对您的方案有帮助,值得注意的是,您可以使用 Powershell也进行编码转换。
When you redirect output to a file, Powershell is using Unicode as the default encoding. Instead of using the redirection operator, you can pipe to
Out-File
with a-Encoding UTF8
switch.The following TechNet article has more information (equivalent to
Get-Help Out-File -full
in Powershell v2).In case it helps your scenario at all, it's worth noting that you can use Powershell to do the encoding conversion also.