views:

139

answers:

1

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

A: 

> essentially is using the Out-File cmdlet who's default encoding is Unicode. Try:

iconv.exe ... | Out-File -Encoding Utf8

or with params:

& "C:\Program Files\GnuWin32\bin\iconv.exe" -f iso-8859-1 -t utf-8 $inFileName |
   Out-File -Encoding Utf8 $outFileName 

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):

[Console]::OutputEncoding = [Text.Encoding]::UTF8 
Keith Hill
Thank you for your response Keith.I have just tried. Now the output file is UTF-8 encoded, but some characters are broken (ñ and á, for example). In the iconv output they are OK (al least I see they are good when using my BAT version). Any idea about it?
fdediego
You need to tell PowerShell what encoding iconv.exe is using. Set the console output encoding so PowerShell knows how to interpret the bytes coming out of iconv.exe - `[Console]::OutputEncoding = [Text.Encoding]::UTF8` - as discussed here http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!7212.entry
Keith Hill
Thank you very much Keith. It is fully working now!
fdediego