Copy content of subfolders with Ant
Asked Answered
A

1

10

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

Auld answered 6/8, 2010 at 12:4 Comment(0)
P
11
<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.

Platino answered 6/8, 2010 at 21:36 Comment(2)
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?Auld
@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.Platino

© 2022 - 2024 — McMap. All rights reserved.