Using foreach

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

Using foreach

by nisse :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I'm trying get all files from a directory and print it to the screen. And I only want to print the name of the file, not with the path and filename. My code looks like this:

<target name="start">
   <foreach target="printOut" param="Files">
      <path>
         <fileset dir="C:/Temp" casesensitive="no" />
      </path>
   </foreach>
</target>

<target name="printOut">
   <echo message="${Files}"/>
</target>


This code prints for example "C:/Temp/MyFile.txt"
But I only want it to print "MyFile.txt", how do I do that in a simple way??

Very grateful for any help!

RE: Using foreach

by Rebhan, Gilbert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


-----Original Message-----
From: nisse [mailto:nisseasp@...]
Sent: Wednesday, June 24, 2009 9:21 AM
To: user@...
Subject: Using foreach

/*

[..]


This code prints for example "C:/Temp/MyFile.txt"
But I only want it to print "MyFile.txt", how do I do that in a simple way??

*/

use <basename>, example

<for param="file">
 <path>
  <fileset dir="C:/test" includes="*.txt"/>
 </path>
<sequential>
 <var name="basename" unset="true"/>
 <basename file="@{file}" property="basename"/>
 <var name="filenames" value="${filenames},${basename}"/>
</sequential>
</for>


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


RE: Using foreach

by nisse :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks! Works great!


Rebhan, Gilbert wrote:
-----Original Message-----
From: nisse [mailto:nisseasp@home.se]
Sent: Wednesday, June 24, 2009 9:21 AM
To: user@ant.apache.org
Subject: Using foreach

/*

[..]


This code prints for example "C:/Temp/MyFile.txt"
But I only want it to print "MyFile.txt", how do I do that in a simple way??

*/

use <basename>, example

<for param="file">
 <path>
  <fileset dir="C:/test" includes="*.txt"/>
 </path>
<sequential>
 <var name="basename" unset="true"/>
 <basename file="@{file}" property="basename"/>
 <var name="filenames" value="${filenames},${basename}"/>
</sequential>
</for>


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

RE: Using foreach

by Rebhan, Gilbert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



-----Original Message-----
From: Rebhan, Gilbert [mailto:Gilbert.Rebhan@...]
Sent: Wednesday, June 24, 2009 9:33 AM
To: 'Ant Users List'
Subject: RE: Using foreach


-----Original Message-----
From: nisse [mailto:nisseasp@...]
Sent: Wednesday, June 24, 2009 9:21 AM
To: user@...
Subject: Using foreach

/*

[..]


This code prints for example "C:/Temp/MyFile.txt"
But I only want it to print "MyFile.txt", how do I do that in a simple way??

*/

wrong copy/paste example in my last post ..

take two =

<for param="file">
 <path>
  <fileset dir="C:/test" includes="*.txt"/>
 </path>
<sequential>
 <var name="basename" unset="true"/>
 <basename file="@{file}" property="basename"/>
 <echo> Filename = ${basename}</echo>
</sequential>
</for>


Regards, Gilbert


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


Parent Message unknown RE: Using foreach

by Matt Benson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Not to rain on the proverbial parade, but you don't need iteration or third-party tasks to do this:

<pathconvert pathsep="${line.separator}">
  <fileset dir="C:/test" includes="*.txt" />
  <flattenmapper />
</pathconvert>

HTH,
Matt

P.S. You also didn't need to wrap your fileset in a path on your iteration-based example.

--- On Wed, 6/24/09, nisse <nisseasp@...> wrote:

> From: nisse <nisseasp@...>
> Subject: RE: Using foreach
> To: user@...
> Date: Wednesday, June 24, 2009, 2:46 AM
>
> Thanks! Works great!
>
>
>
> Rebhan, Gilbert wrote:
> >
> >
> > -----Original Message-----
> > From: nisse [mailto:nisseasp@...]
>
> > Sent: Wednesday, June 24, 2009 9:21 AM
> > To: user@...
> > Subject: Using foreach
> >
> > /*
> >
> > [..]
> >
> >
> > This code prints for example "C:/Temp/MyFile.txt"
> > But I only want it to print "MyFile.txt", how do I do
> that in a simple
> > way??
> >
> > */
> >
> > use <basename>, example
> >
> > <for param="file">
> >  <path>
> >   <fileset dir="C:/test"
> includes="*.txt"/>
> >  </path>
> > <sequential>
> >  <var name="basename" unset="true"/>
> >  <basename file="@{file}"
> property="basename"/>
> >  <var name="filenames"
> value="${filenames},${basename}"/>
> > </sequential>
> > </for>
> >
> >
> > Regards, Gilbert
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@...
> > For additional commands, e-mail: user-help@...
> >
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Using-foreach-tp24179911p24180173.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@...
>
>

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

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


RE: Using foreach

by Rebhan, Gilbert :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



-----Original Message-----
From: Matt Benson [mailto:gudnabrsam@...]
Sent: Wednesday, June 24, 2009 2:57 PM
To: Ant Users List
Subject: RE: Using foreach

/*

Not to rain on the proverbial parade, but you don't need iteration or third-party tasks to do this:

<pathconvert pathsep="${line.separator}">
  <fileset dir="C:/test" includes="*.txt" />
  <flattenmapper />
</pathconvert>

HTH,
Matt

P.S. You also didn't need to wrap your fileset in a path on your iteration-based example.

*/

sure, works also with <pathconvert> and flattenmapper, would be better
in this case, but he already used antcontrib in his example, so  ...
yes, <path> is redundant,  it was a quick copy paste
of antcontrib manual, so the antcontrib manual has to be fixed.

btw, just noticed i completely ignored the fact that the poster used <foreach>,
so var and unset is not needed, as foreach opens a new project scope for every loop =

<target name="start">
  <foreach target="printOut" param="Files">
    <fileset dir="C:/test" casesensitive="no" />
  </foreach>
</target>

<target name="printOut">
  <basename file="${Files}" property="basename"/>
  <echo message="${basename}"/>
</target>



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