ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Francis GALIEGUE <...@one2team.com>
Subject Re: Ant find duplicate files
Date Fri, 06 Nov 2009 22:40:38 GMT
On Fri, Nov 6, 2009 at 22:45, cvsusr <spers@rediffmail.com> wrote:
>
> Hi
>
> In my ant build,
>
> I need to find duplicate files under set of dirs and if found, copy it to
> the temp directory.
>
> say in my current dir i have chk/src/a.txt and chk/src2/a.txt and
> chk/src2/b.txt
>
> the task should find a.txt under src and src2 and copy it to chk/test dir
> with folder names
>
> Can this  be done in any way..
>

You know, there's no need to do a double post... Well, anyway, as you
still don't have a workable answer so far, here is the solution that I
envisioned. Quoting your original mail:

> need to find duplicate files under set of dirs and if found, copy it to
> the temp directory.
>
> say in my current dir i have chk/src/a.txt and chk/src2/a.txt and
> chk/src2/b.txt
>
> the task should find a.txt under src and src2 and copy it to chk/test dir
> with folder names
>
> Can this  be done in any way..

What do you call "folder names"? Give an example, please. Until then,
the following is assumed here:

* that since src/a.txt exists but not src/b.txt, what you want as a
result is chk/test/a.txt, but not chk/test/b.txt;
* that there's only one level deep (ie, there won't be src/a/b.txt and
src2/a/b.txt).

If this is not the case, the below example will NOT work, and you'll
have to be more precise about what you really want. I have worked with
much more complicated schemes than that wrt filenames, and have found
ant-contrib to be of outstanding value for such cases. Base ant just
cannot do it that easily, or in such a convoluted way that trying to
get it to work is a nightmare. Honestly.

Anyway, here is what I'd do in your case:

<property name="srcdir" value="chk/src2"/>

<fileset id="origfiles" dir="${srcdir}" includes="*.txt"/>

<target name="doit">
    <for param="origfile">
        <path>
            <fileset refid="origfiles"/>
        </path>
        <sequential>
            <propertyregex value="dup" override="yes"
input="@{origfile}" regexp="/src2/" replace="/src/"/>
            <if>
                <available file="${dup}" type="file"/>
                <then>
                    <!-- Yes, mkdir each time. It's not that much of a
waste, really, since <mkdir> will silently ignore an existing
directory -->
                    <mkdir dir="chk/test"/>
                    <copy file="@{origfile}" todir="chk/test"/>
                </then>
            </if>
        </sequential>
    </for>
</target>

This uses three tasks from ant-contrib: for, if/then, propertyregex.
And it's only a simple usage: <propertyregex> is ant's Swiss army
knife for regexes and beats every other regex mechanism in ant.

--
Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message