PowerShellscript, bad file encoding conversation

前端 未结 1 1005
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 03:41

I have a PowerShell script for the conversation of file character encoding.

Get-ChildItem -Path D:/test/data -Recurse -Include *.txt |
ForEach-Object {
  $in         


        
1条回答
  •  醉梦人生
    2020-12-21 04:02

    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.

    E:\bin\iconv\iconv.exe -f cp1251 -t utf-8 $inFileName | Out-File -FilePath $outFileName -Encoding UTF8
    

    The following TechNet article has more information (equivalent to Get-Help Out-File -full in Powershell v2).

    • Out-File

    In case it helps your scenario at all, it's worth noting that you can use Powershell to do the encoding conversion also.

    Get-Content $inFileName -Encoding ASCII |
    Out-File -FilePath $outFileName -Encoding UTF8
    

    0 讨论(0)
提交回复
热议问题