November 30, 2014

Ant Script to list xml files recursively from specified directory

Here is the code-snippet to list the files recursively in the specified directory using <foreach>, <path> &  <fileset>

<project name="ListFilesProject" default="listfiles" basedir=".">
    <description>
           List Files recursively
    </description>
  <!-- set global properties for this build -->
  <property name="src.dir" value="D:\src" />
 
  <target name="listfiles" description="list the files" >
   <echo message="listfiles xml files in the given directory"/>
   <taskdef resource="net/sf/antcontrib/antlib.xml"/>
   <foreach target="list_xml_file" param="xml_File" >
         <path>
             <fileset dir="${src.dir}" casesensitive="yes" includes="**/*.xml" />
         </path>
    </foreach>
  </target>
 
  <target name="list_xml_file">
          <echo message="${xml_File}"/>
  </target>
 
</project


Note:

Usage of <foreach> requires ant-contrib-1.0b2.jar. Download it from http://mirrors.ibiblio.org/maven/ant-contrib/jars/ant-contrib-1.0b2.jar place it under <Ant Installation Dir>\ant\lib

*.xml       ->  lists all the xml files from the specified directory
**/*.xml  - > lists all the xml files from all the sub-directories under specified directory

The * matches all files in the directory while ** matches the all the files in the entire directory tree.

No comments:

Post a Comment