1

I have a folder structure like

ant/
    test1/
        images/
            1.png ...
    test2/
        images/
            121.png ...
    test3/
        images/
            173.gif ...

I want to read all the images from different different images folders and put them into one image folder under the ant folder.

I have written this code

<project name = "java-ant project" default = "copy-file">  
    <target name="copy-file">  
        <copy todir="D:/ant/images">
            <fileset includes="**/images/" dir="D:/ant/"/></copy>  
    </target>  
</project> 

This copies successfully into the images folder, but keeps the source folder structure.

Like:

ant/
    images/
        test1/
            images/
                1.jpg ...
        test2/
            images/
                121.jpg ...

My desired output is:

ant/ 
    images/
        1.jpg
        121.png
        173.gif
        ...
martin clayton
  • 76,436
  • 32
  • 213
  • 198
Rajendra Bar
  • 49
  • 1
  • 8
  • See also: https://stackoverflow.com/questions/3423555/copy-content-of-subfolders-with-ant but the answer there uses a regex which you don't need for a simple flatten. – martin clayton Mar 17 '21 at 12:52

1 Answers1

0

You can use something like this:

<property name="src.dir" value="D:/ant/"/>
<property name="dest.dir" value="D:/ant/images"/>

<copy todir="${dest.dir}">
  <fileset dir="${src.dir}" includes="**/images/*">
    <type type="file"/>
  </fileset>
  <mapper type="flatten" />
</copy>

The flatten mapper is the key part.

The type="file" element ensures that only files are copied. Without that you might also see (empty) directories copied.

martin clayton
  • 76,436
  • 32
  • 213
  • 198