Alternative for <available> taks to check a pattern of file existence

View: New views
12 Messages — Rating Filter:   Alert me  

Alternative for <available> taks to check a pattern of file existence

by cvsusr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Can anyone please help me..

Thanks in advance.




Re: Alternative for <available> taks to check a pattern of file existence

by Francis Galiegue-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 29, 2009 at 12:00, cvsusr <spers@...> wrote:

>
> 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..
>
> Can anyone please help me..
>
> Thanks in advance.
>

One option would be to use an appropriate <fileset> and loop over it
with <for>, but <for> requires ant-contrib.

--

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@...
40 avenue Raymond Poincaré
75116 Paris

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


Re: Alternative for <available> taks to check a pattern of file existence

by cvsusr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Thanks for your reply..

I know to use Fileset.. but how to check with forloop.. if possible, Can you please give me an example? Im new to this concept and hence requesting your help..

Thanks

Francis Galiegue-4 wrote:
On Thu, Oct 29, 2009 at 12:00, cvsusr <spers@rediffmail.com> wrote:
>
> 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..
>
> Can anyone please help me..
>
> Thanks in advance.
>

One option would be to use an appropriate <fileset> and loop over it
with <for>, but <for> requires ant-contrib.

--

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

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

Re: Alternative for <available> taks to check a pattern of file existence

by cvsusr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I tried this.. this is printing the *.txt file that exist in the current folder
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<for param="file">
  <path>
    <fileset dir="." includes="*.txt"/>
  </path>
<sequential>
<echo> file list::: @{file} </echo>
</sequential>
</for>


Now i need to check if the file exist then perform the target..  Do i need to use condition for this??

Thanks
Sri
Francis Galiegue-4 wrote:
On Thu, Oct 29, 2009 at 12:00, cvsusr <spers@rediffmail.com> wrote:
>
> 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..
>
> Can anyone please help me..
>
> Thanks in advance.
>

One option would be to use an appropriate <fileset> and loop over it
with <for>, but <for> requires ant-contrib.

--

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

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

RE: Alternative for <available> taks to check a pattern of file existence

by Rebhan, Gilbert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 

-----Original Message-----
From: cvsusr [mailto:spers@...]
Sent: Thursday, October 29, 2009 12:00 PM
To: user@...
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@...
For additional commands, e-mail: user-help@...


RE: Alternative for <available> taks to check a pattern of file existence

by Rebhan, Gilbert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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@...]
Sent: Thursday, October 29, 2009 12:00 PM
To: user@...
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@...
For additional commands, e-mail: user-help@...


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


Re: Alternative for <available> taks to check a pattern of file existence

by Francis Galiegue-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 29, 2009 at 13:00, cvsusr <spers@...> wrote:

>
> I tried this.. this is printing the *.txt file that exist in the current
> folder
> <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
> <for param="file">
>  <path>
>    <fileset dir="." includes="*.txt"/>
>  </path>
> <sequential>
> <echo> file list::: @{file} </echo>
> </sequential>
> </for>
>
>
> Now i need to check if the file exist then perform the target..  Do i need
> to use condition for this??
>

Well, no, since if there's no file, there will be no iteration at
all... Hence my suggestion. A fileset can be empty.

--

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@...
40 avenue Raymond Poincaré
75116 Paris

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


Re: Alternative for <available> taks to check a pattern of file existence

by cvsusr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Francis Galiegue  & Gilbert ,

Thanks a lot for replying. I got it done taking inputs from all your comments.
Below is the code that is working fine.


<target name="check-abc">
<for param="file">
  <path>
    <fileset id="file.exist" dir="." includes="*.txt"/>
  </path>
<sequential>
<echo> file list::: @{file} </echo>
<available file="@{file}" property="abc.present"/>
</sequential>
</for>
</target>

<target name="do-if-abc" depends="check-abc" if="abc.present">
<echo> true</echo>
</target>

Thanks a lot again for helping.

Regards
Sri

Francis Galiegue-4 wrote:
On Thu, Oct 29, 2009 at 13:00, cvsusr <spers@rediffmail.com> wrote:
>
> I tried this.. this is printing the *.txt file that exist in the current
> folder
> <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
> <for param="file">
>  <path>
>    <fileset dir="." includes="*.txt"/>
>  </path>
> <sequential>
> <echo> file list::: @{file} </echo>
> </sequential>
> </for>
>
>
> Now i need to check if the file exist then perform the target..  Do i need
> to use condition for this??
>

Well, no, since if there's no file, there will be no iteration at
all... Hence my suggestion. A fileset can be empty.

--

Francis Galiegue
ONE2TEAM
Ingénieur système
Mob : +33 (0) 683 877 875
Tel : +33 (0) 178 945 552
fge@one2team.com
40 avenue Raymond Poincaré
75116 Paris

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

RE: Alternative for <available> taks to check a pattern of file existence

by Rebhan, Gilbert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 

-----Original Message-----
From: cvsusr [mailto:spers@...]
Sent: Thursday, October 29, 2009 3:34 PM
To: user@...
Subject: Re: Alternative for <available> taks to check a pattern of file existence

/*
<target name="check-abc">
<for param="file">
  <path>
    <fileset id="file.exist" dir="." includes="*.txt"/>
  </path>
<sequential>
<echo> file list::: @{file} </echo>
<available file="@{file}" property="abc.present"/>
</sequential>
</for>
</target>
*/

using for - which requires antcontrib also - when all you want
to know is whether there are any files with a specific extension
or name is redundant; all you need is a fileset and a condition

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

<target name="check-abc">
<condition property="abc-present">
  <not>
    <equals arg1="${toString:checkdir}" arg2="" />
  </not>
</condition>
</target>

<target name="do-if-abc" depends="check-abc" if="abc.present">
<echo> true</echo>
</target>


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


RE: Alternative for <available> taks to check a pattern of file existence

by cvsusr :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Gilbert,

Thanks..

I replace the code with this.. its working fine..


Thanks for correcting me..

Regards
Sri
Rebhan, Gilbert wrote:
 

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

/*
<target name="check-abc">
<for param="file">
  <path>
    <fileset id="file.exist" dir="." includes="*.txt"/>
  </path>
<sequential>
<echo> file list::: @{file} </echo>
<available file="@{file}" property="abc.present"/>
</sequential>
</for>
</target>
*/

using for - which requires antcontrib also - when all you want
to know is whether there are any files with a specific extension
or name is redundant; all you need is a fileset and a condition

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

<target name="check-abc">
<condition property="abc-present">
  <not>
    <equals arg1="${toString:checkdir}" arg2="" />
  </not>
</condition>
</target>

<target name="do-if-abc" depends="check-abc" if="abc.present">
<echo> true</echo>
</target>


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

Re: Alternative for <available> taks to check a pattern of file existence

by Matt Benson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Okay, the idea of using a fileset to specify possible matches by pattern is right.  All you have to add to that is the use of the resourcecount task/condition to count the number of files matched.  If you're using a version of Ant earlier than 1.7.0 you can use <pathconvert setonempty="false"> with an <isset> condition to determine a fileset's emptiness.

-Matt

--- On Thu, 10/29/09, cvsusr <spers@...> wrote:

> From: cvsusr <spers@...>
> Subject: Alternative for <available> taks to check a pattern of file existence
> To: user@...
> Date: Thursday, October 29, 2009, 6:00 AM
>
> 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..
>
> Can anyone please help me..
>
> Thanks in advance.
>
>
>
>
> --
> View this message in context: http://www.nabble.com/Alternative-for-%3Cavailable%3E-taks-to-check-a-pattern-of-file-existence-tp26110901p26110901.html
> Sent from the Ant - Users mailing list archive at
> Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@...
> For additional commands, e-mail: user-help@...
>
>


     

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


Re: Alternative for <available> taks to check a pattern of file existence

by Stefan Bodewig :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2009-10-29, cvsusr <spers@...> wrote:

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

Use the condition task with a resourcecount condition to ste a property
and make your target use that property.

Stefan

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