Ant - copy only file not directory

岁酱吖の 提交于 2019-12-18 12:27:52

问题


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,


回答1:


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.




回答2:


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.




回答3:


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>



回答4:


Can you try

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

** is used to match a directory structure.




回答5:


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



回答6:


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>



回答7:


The secret is to use not fileset but dirset instead.



来源:https://stackoverflow.com/questions/1564763/ant-copy-only-file-not-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!