Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant - copy only file not directory

Tags:

ant

I need to copy all files in a folder except directory in that folder using Ant script.

Im using below script to do that.

<copy todir="targetsir">
  <fileset dir="srcdir">
     <include name="**/*.*"/>
  </fileset>
</copy>

But it copies all files and directory in that folder.

how to restrict/filter directory in that folder?

thanks,

like image 716
Srinivasan Avatar asked Oct 14 '09 07:10

Srinivasan


3 Answers

I think there is an easier way.

flatten="true" - Ignore directory structure of source directory, copy all files into a single directory, specified by the todir attribute. The default is false.

like image 132
Neo Avatar answered Nov 13 '22 22:11

Neo


Do you mean that srcdir conatins sub-directories, and you you don't want to copy them, you just want to copy the files one level beneath srcdir?

<copy todir="targetsir">
  <fileset dir="srcdir">
     <include name="*"/>
     <type type="file"/>
  </fileset>
</copy>

That should work. The "**/*.*" in your question means "every file under every sub directory". Just using "*" will just match the files under srcdir, not subdirectories.

Edited to exclude creation of empty subdirectories.

like image 10
skaffman Avatar answered Nov 13 '22 23:11

skaffman


I do not have enough reputation to comment, so I'm writing new post here. Both solutions to include name="*" or name="*.*" work fine in general, but none of them is exactly what you might expect.

The first creates empty directories that are present in the source directory, since * matches the directory name as well. *.* works mostly because a convention that files have extension and directories not, but if you name your directory my.dir, this wildcard will create an empty directory with this name as well.

To do it properly, you can leverage the <type /> selector that <fileset /> accepts:

<copy todir="targetsir"> 
  <fileset dir="srcdir"> 
     <include name="*"/> 
     <type type="file"/>
  </fileset> 
</copy>
like image 7
Pavel Avatar answered Nov 14 '22 00:11

Pavel