4

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

Community
  • 1
  • 1
Larsing
  • 171
  • 4
  • 13

4 Answers4

3
for f in `find . -type f`; do
    iconv -f cp1252 -t utf-8 $f > $f.converted
    mv $f.converted $f
done
neevek
  • 11,760
  • 8
  • 55
  • 73
  • Thanx, this works as intended! But, have you got any idea why the find-exec expression doesn't work? Is it to do with the output redirection..? – Larsing Mar 14 '12 at 08:29
  • I am not sure, but `-exec` is not preferred. from somewhere I learnt `-exec` can't execute commands that have long arguments. I use `xargs` instead. – neevek Mar 14 '12 at 11:45
1

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...

Larsing
  • 171
  • 4
  • 13
0

The error in the first try is that the redirection operator '>' ist evaluated by the shell before find starts.

The error in the second try is that the text between the single quotes is interpreted as the name of a command that is to be executed by find, but that doesn't exist.

In your working solution the first command to be executed by find is a subshell, and the options are enclosed in single quotes, so they are not interpreted by the outer shell but by the subshell.

Mike11
  • 37
  • 2
0

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

Murray McDonald
  • 631
  • 4
  • 5