ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Oltmans" <joltm...@bolosystems.com>
Subject RE: Simple if/else Condition
Date Thu, 19 Jul 2007 00:04:55 GMT
A simpler way is to do the following:

<length property="num.files">
  <fileset dir="dir-to-check" followsymlinks="false"/>
</length>

OR if using it inside of <condition> or <if>:

<if>
  <length length="0" when="greater">
  <fileset dir="dir-to-check" followsymlinks="false"/>
</length>
  <then>
     <!-- Code to execute when more than 1 file is present -->
  </then>
  <else>
     <!-- code to execute when dir is empty -->
  </else>
</if>

One note, but this might not work too well on older ant versions.
<length> was added as a condition in ant 1.6.3 and I have had some
issues with 1.6.5.

Hope that helps.
James

-----Original Message-----
From: David Weintraub [mailto:qazwart@gmail.com] 
Sent: Wednesday, July 18, 2007 9:37 AM
To: Ant Users List
Subject: Re: Simple if/else Condition

That's what I get for answering mail past midnight after my brain goes
to sleep. I mean "target" and not "task".

Hmmm. How to tell whether a directory is empty...

You could do this:

<delete dir="mydirectory"/>
<mkdir dir="mydirectory"/>

Then, the answer will be yes. No good.

I haven't tried this, but it might work:

1). Create a Path that contains the files in the directory you want to
test.

<path id="testpath">
<fileset dir="mydirectory"/>
</path>

2). Convert the path to a property:

<property name="myproperty" refid="testpath"/>

3). Now compare that property to an empty string

<condition property="is_directory_empty">
     <isequal arg1="${myproperty}" arg2="" trim="true"/>
</condition>

The theory is that if there are no files in the directory, then the
PATH will be empty.

On 7/18/07, Evan J <maps.this.address@gmail.com> wrote:
> Thank you Qazwart, except that we don't have `task' task but rather
> `target' for antcall. Either way, it should get the job done.
>
> Now I have to figure out to see how I can check whether a directory
> (and its subdirectories) are empty -- Length doesn't help.
>
> On 7/18/07, Qazwart <qazwart@gmail.com> wrote:
> >
> > On Jul 17, 2007, at 11:18 PM, Evan J wrote:
> > > if (condition)
> > >  perform some task, i.e. echo message="condition met"
> > > else
> > >  perform another task
> >
> > Check out the AntContrib tasks at
<http://ant-contrib.sourceforge.net/
> > tasks/tasks/index.html>.
> > There is actually an <if> ant task at <http://ant-
> > contrib.sourceforge.net/tasks/tasks/if.html>. I've used it quite
> > successfully with Ant 1.7.
> >
> > If you don't want to use optional tasks, you can use the <condition>
> > task like this to emulate an <if><else> task:
> >
> > <condition
> >      property="os_is_windows"
> >      value="true">
> >      <os family="windows"/>
> > </condition>
> > <antcall task="windows_task"/>
> > <antcall task="unix_task"/>
> >
> > <task name="windows_task if="os_is_windows">
> >      <blah>
> >      </blah>
> > </task>
> >
> > <task name="unix_task" unless="os_is_windows">
> >      <blah>
> >      </blah>
> > </task>
> >
> > David Weintraub
> > david@weintraub.name
> > qazwart@gmail.com
> >
> >
> >
> >
---------------------------------------------------------------------
> > 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
>
>


-- 
--
David Weintraub
qazwart@gmail.com

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