Srinivasan
Srinivasan

Reputation: 12040

Ant - copy only file not directory

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,

Upvotes: 12

Views: 22813

Answers (7)

Rajkumar A
Rajkumar A

Reputation: 1

If your folder has many subdirectories and you don't want them to be copied (if you want only files) try this..

<target name="copy">
<copy todir="out" flatten="true">
<fileset dir="tn">
<filename name="**/cit.txt" />
</fileset>
</copy>
</target>

Upvotes: 0

skaffman
skaffman

Reputation: 403481

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.

Upvotes: 11

Pavel
Pavel

Reputation: 446

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>

Upvotes: 7

Neo
Neo

Reputation: 251

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.

Upvotes: 25

btpka3
btpka3

Reputation: 3830

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

Upvotes: 1

dfh
dfh

Reputation: 1

The secret is to use not fileset but dirset instead.

Upvotes: -1

Rahul
Rahul

Reputation: 13056

Can you try

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

** is used to match a directory structure.

Upvotes: 1

Related Questions