-----Original Message-----
From: Alexei Alexei [mailto:a_subscri...@mail.ru]
Sent: Monday, December 29, 2008 11:21 AM
To: Rebhan, Gilbert
Subject: RE: Ant- copy file from one folder to many folders

/*

The global task is:

In the folder "myfolder1" I have file "myclass.class".
1. Find file "myclass.class" in the folder "myfolder2" and all it's subfolders. 
(E.g. return list of folders)
2. If file is fonded in the folder "myfolder2" (or any of it's subfolders) then 
copy file "myclass.class" from the "myfolder1" to all this folders.
3. If not fouded then don't copy

Can I do this with Ant-contrib?

*/

Yes, just put in available condition before copy =

<project name="bla" default="main" basedir=".">

<!-- // Taskdefs -->
<!-- Import AntContrib -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Taskdefs // -->

<target name="main">
 <for param="dir">
  <dirset dir="C:/test" includes="**"/>
   <sequential>
    <if>
     <available file="@{dir}/myclass.class"/>
      <then>
       <copy todir="@{dir}" overwrite="true" verbose="true">
        <fileset dir="C:/test/myfolder1" includes="myclass.class" />
       </copy>
      </then>
    </if>
   </sequential>
 </for>
</target>

</project>

btw. its not necessary to exclude the myfolder1/myclass.class,
you will see something like =

Skipping self-copy of C:\test\myfolder1\myclass.class

in your output on stdout


Regards, Gilbert


P.S. :
please keep the thread on the list, it maybe useful for others
with the same / a similar problem





-----Original Message-----
From: David Weintraub [mailto:qazw...@gmail.com]
Sent: Monday, December 29, 2008 6:10 AM
To: Ant Users List; Alexei Alexei
Subject: Re: Ant- copy file from one folder to many folders

/*

The standard <copy> task requires a single target name, so you couldn't do it 
with the standard Ant tasks. However, there is a special addition to Ant called 
the AntContrib tasks <http://ant-contrib.sourceforge.net/>. These extend Ant to 
allow for all sorts of tricks.

There's one special AntContrib task called <foreach> 
<http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html>. This will allow 
you to go through each directory in a list, and use that as the "todir" 
parameter of a copy task. It'll look something like this:

<target name="orig">
    <foreach
        target="copyclass"
        parameter="copy.target">
        <path>
             <dirset dir="myfolder*/**"/>
        </path>
    </foreach>
</target>

<target name="copy">
    <copy todir="${copy.target}"
        file="myclass.class"/>
</target>

READ THE COMPLETE DOCUMENTATION ON THE ANT-CONTRIB PACKAGE! You must download 
the AntContrib JAR file, put it in your Ant's "lib"
directory, and add a <taskdef> task to your build.xml file.

*/

Better use <for> instead of <foreach>, as foreach opens a new project scope for 
every target execution.

Also with <for> there's no need for an extra target.
So with a given structure like that =

c:\test>tree /A /F
C:.
+---myfolder1
|       myclass.class
|
\---myfolder2
    |   myclass.class
    |
    +---test
    |   |   myclass.class
    |   |
    |   \---test2
    |           myclass.class
    |
    \---test3
            myclass.class

you can use =

<project name="foobar" default="main" basedir=".">

<!-- // Taskdefs -->
<!-- Import AntContrib -->
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Taskdefs // -->

<target name="main">
 <for param="dir">
  <dirset dir="C:/test" includes="**"/>
   <sequential>
    <copy todir="@{dir}" overwrite="true" verbose="true">
     <fileset dir="C:/test/myfolder1" includes="myclass.class" />
    </copy>
   </sequential>
 </for>
</target>

</project>


btw Alexei =
your given situation was a bit irritating to me, as the file myclass.class is 
already existent in all directories, including the one you want to copy from
(myfolder1 in your example).
i understood it like that = a new myclass.class drops in myfolder1 and you need 
to spread it recursive in all subdirectories, right !?


Regards, Gilbert

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



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

Reply via email to