Bulk convert cp1252 to utf-8 in Windows

2019-04-09 18:41发布

问题:

So, I've been trying to convert a large java source tree from cp1252 to UTF-8 in Windows, using tips and trix I've found online, specificly here. Problem is, I'm on Windows; I don't do VB; Cygwin's iconv doesn't take the -o switch.

The line I first tried to use is:

find . -type f -print -exec iconv -f cp1252 -t utf-8 {} > {}.converted \; -exec mv {}.converted {} \;

This creates a file {}.converted in the working directory and the second -exec fails for obvious reasons.

Putting quotes around the iconv expression:

find . -type f -print -exec 'iconv -f cp1252 -t utf-8 {} > {}.converted' \; -exec mv {}.converted {} \;

resulsts in the folowing error:

find: `iconv -f cp1252 -t utf-8 ./java/dv/framework/activity/model/ActivitiesMediaViewImpl.java > ./java/dv/framework/activity/model/ActivitiesMediaViewImpl.java.converted': No such file or directory

though executing the individual expressions by hand works perfectly.

I've experimented with random quoting but nothing seems to work, what am I missing? Why won't it work..?

Thanx in advance, Lars

回答1:

for f in `find . -type f`; do
    iconv -f cp1252 -t utf-8 $f > $f.converted
    mv $f.converted $f
done


回答2:

Allright, once again answering my own question (this is starting to become a bad habit...)

Allthough there is nothing wrong with Neevek's solution, the perfectionist in me wants to get the find -exec expression right. Wrapping the iconv statement in a sh -c '...' does the trick:

find . -type f -print -exec sh -c 'iconv -f cp1252 -t utf-8 {} > {}.converted' \; -exec mv {}.converted {} \;

Still, the underlying question of why there is a problem using i/o redirection in find -exec statements remains unresolved...



回答3:

I haven't used Cygwin very much but there's a "native" windows version of Iconv that I use all the time. Here's an excerpt from a batch file that i use to convert all the files in a sub-dir from HP-ROMAN8 encoding to UTF-8 encoding -- putting the result './temp" under the originals:

@set dir=original

@set ICONV="C:\Program Files (x86)\iconv-1.9.2.win32\bin\iconv"

if EXIST .\%dir%\temp ( erase .\%dir%\temp*.* /Q @if ERRORLEVEL 1 (@echo Unable to erase all files from the "temp" sub-directory @goto THE_END ) ) else ( mkdir .\%dir%\temp @if ERRORLEVEL 1 (@echo Unable to create the "temp" sub-directory @goto THE_END ) )

for %%f IN (./%dir%/*.xml) do ( %ICONV% -f HP-ROMAN8 -t UTF-8 "./%dir%/%%f" > "./%dir%/temp/%%f" if ERRORLEVEL 1 (goto ICONV_ERROR) )