ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ivan Ivanov <>
Subject Re: Counting files in a dir
Date Tue, 05 Apr 2005 08:56:19 GMT
Hello, Fermin,
You can do 2. with javascript:
  <scriptdef name="countFiles" language="javascript">
    <attribute name="dir"/>
    <attribute name="extension"/>
    <attribute name="property"/>
      f = new File(attributes.get("dir"));
      e = attributes.get("extension");

      filter = new FileFilter() {
        accept: function(file) {
          s = file.getName();
          if (s.endsWith(e)) {
            return true;

      c = f.listFiles(filter).length;
project.setNewProperty(attributes.get("property"), c);

  <countFiles dir="${basedir}" property="xmlfiles"
  <echo>$${xmlfiles} is ${xmlfiles}</echo>
  <countFiles dir="${basedir}" property="txtfiles"
  <echo>$${txtfiles} is ${txtfiles}</echo>
  <condition property="prop">
    <equals arg1="${txtfiles}" arg2="2"/>
  <fail unless="prop" message="txt files count does
not match"/>

In fact, I often need to extract information about
files and I thought to do a "lib" of <scriptdef>s like
the one above to improve reusability. However, I wrote
an ordinary Ant task that does the job, because it is
easier for me to deploy (plus I am not very good at
javascript). You can see this task at [1].



--- Fermin Da Costa Gomez <>
> Hi,
> What would be the best way to do the following:
> 1. Going into a directory (this is ok)
> 2. Counting the number of files of type x (getting
> files is ok, *counting* is an 
> issue)
> 3. Depending on the count do something (break out of
> the target)
> Items 2. and 3. are bugging me somewhat (esp the
> counting bit in 2).
> Does anybody have a suggestion as to how i can
> accomplish this?
> tia,
> Fermin DCG
> To unsubscribe, e-mail:
> For additional commands, e-mail:

Yahoo! Messenger 
Show us what our next emoticon should look like. Join the fun.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message