ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ivan Ivanov <rambiusparkisan...@yahoo.com>
Subject Re: Counting files in a dir
Date Tue, 05 Apr 2005 08:56:19 GMT
Hello, Fermin,
You can do 2. with javascript:
<project>
  <scriptdef name="countFiles" language="javascript">
    <attribute name="dir"/>
    <attribute name="extension"/>
    <attribute name="property"/>
    <![CDATA[
      importClass(java.io.File);
      importClass(java.io.FileFilter);
      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);
    ]]>
  </scriptdef>

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

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].

HTH
Ivan

[1]http://marc.theaimsgroup.com/?l=ant-user&m=110987001620450&w=2

--- Fermin Da Costa Gomez <f.dacosta.g@gmail.com>
wrote:
> 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:
> user-unsubscribe@ant.apache.org
> For additional commands, e-mail:
> user-help@ant.apache.org
> 
> 


		
__________________________________ 
Yahoo! Messenger 
Show us what our next emoticon should look like. Join the fun. 
http://www.advision.webevents.yahoo.com/emoticontest

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message