tags:

views:

74

answers:

1

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

+1  A: 
<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.

Alexander Pogrebnyak
Thanks a lot, Alexander!I've changed regexp a little, because in your variant it acted similar to flatten. But after changing regexpmapper to <regexpmapper from="^([^/]*)/(.*)$$" to="\2" handledirsep="true"/>,I've got what I need. Could you please correct mapper in your answer?
Yury Khrol
@Yury. I guess I did not test a deep enough hierarchy. Because the `to` parameter is really simple, I have only left 1 regex group, but changed to `[^/]*` at the beginning of the expression.
Alexander Pogrebnyak