Copy content of subfolders with Ant

Yury Khrol picture Yury Khrol · Aug 6, 2010 · Viewed 9.4k times · Source

How can I copy content of all subfolders of given folder using Ant?

i.e. I have such folder structure

folder/
folder/sub1/1.txt
folder/sub1/f1/1.txt
folder/sub2/2.txt
...

I don't know exact names of subfolders. And I need to copy content from all of them into one folder (keeping the structure of content, i.e. copying all files into one dir using flatten isn't a solution). I need to get

newfolder/1.txt
newfolder/1/1.txt
newfolder/2.txt
...

Does fileset allows to group subfolders in such a way? ** stands for zero or more directories, and usage of * as directory name is disallowed, i.e. <fileset dir="${dir}/*/" /> isn't acceptable.

Thanks in advance, Yury

Answer

Alexander Pogrebnyak picture Alexander Pogrebnyak · Aug 6, 2010
<copy toDir="newfolder">
  <fileset dir="folder">
    <include name="*/**"/>
    <exclude name="*"/>
  </fileset>
  <regexpmapper from="^[^/]*/(.*)$$" to="\1" handledirsep="true"/>
</copy>

You only need to specify handledirsep if you ever intend to run this script in Windows.