ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rebhan, Gilbert" <Gilbert.Reb...@huk-coburg.de>
Subject RE: Alternative for <available> taks to check a pattern of file existence
Date Thu, 29 Oct 2009 13:00:11 GMT

P.S. : 

you may also check for existence of specific files
via fileset if that is enough for your purpose =

<fileset dir="your/path" includes="**/*.txt" id="checkdir"/>

<condition property="nofiles">
 <equals arg1="${toString:checkdir}" arg2="" />
</condition>

and then f.e. =

<fail if="nofiles" message="No TxtFiles around !!" />

or

<target name="foo" if="nofiles">
..

or

<target name="foo" unless="nofiles">


Regards, Gilbert


-----Original Message-----
From: cvsusr [mailto:spers@rediffmail.com] 
Sent: Thursday, October 29, 2009 12:00 PM
To: user@ant.apache.org
Subject: Alternative for <available> taks to check a pattern of file existence

/*
Hi,

I need to check for existence of files with some extension say *.txt in a
folder and if present execute a target..

I found only <available> task which checks for the existence of single file
only when the name of the file known. I wanted to check whether *.txt is
present or not.. if present execute a target..
*/

one possible solution, the extension you're are looking for is
part of the glob attribute =

your/path/**/*.yourextension means recursive
your/path/*.yourextension means non recursive

examplescript, look for files with .xml extension in C:/Temp
and set property with number of found files
the condition checks whether there are *.xml files in C:/Temp
 - means property set by scriptdef != 0
and then your targets use if / unless with condition property

<project name="bla" default="main" basedir=".">

  <scriptdef name="filecount" language="ruby">
    <attribute name="glob"/>
    <attribute name="property"/>
    $project.setProperty "#{$attributes.get('property')}",
      Dir.glob("#{$attributes.get('glob')}").length.to_s
  </scriptdef>

  <target name="checkfiles">
    <filecount glob="C:/Temp/**/*.xml" property="xmlfiles"/>
    <condition property="files">
      <not>
        <equals arg1="${xmlfiles}" arg2="0" />
      </not>
    </condition>
  </target>

  <target name="foo" if="files">
    <echo>${xmlfiles} files found ..</echo>
  </target>

  <target name="foobar" unless="files">
    <echo>no files found ..</echo>
  </target>

  <target name="main" depends="checkfiles,foo,foobar"/>
</project>


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


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


Mime
View raw message