Bulk convert cp1252 to utf-8 on Windows

So, I was trying to convert a large java source tree from cp1252 to UTF-8 on Windows using the tips and tricks I found on the internet, specifically here . The problem is I'm on Windows; I am not doing VB; Cygwin iconv doesn't accept a switch -o


I first tried using the line:

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

works for obvious reasons.

Putting quotes around the iconv expression:

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


returns the following 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


although executing individual expressions by hand works fine.

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

Thanx in advance, Lars


source to share

3 answers

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




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

Even though there is nothing wrong with Neevek's solution, the perfectionist in me wants to get the correct find -exec expression. Ending iconv's statement sh -c '...'

does the trick:

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


However, the main question why there is a problem using I / O redirection in find -exec operations remains unresolved ...



I haven't used Cygwin very much, but there is a native Windows version of Iconv that I use all the time. Here is an excerpt from the batch file I use to convert all files in the HP-ROMAN8 encoded subdirectory to UTF-8 encoding - put the "./temp" result 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 in "temp" subdirectory @goto THE_END)) else (mkdir. \% dir% \ temp @if ERRORLEVEL 1 (@echo Unable to create subdirectory "temp" @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))



All Articles