# How get filename from FTP::list

**URL:** <https://rubytalk.org/t/how-get-filename-from-ftp-list/41437>\
**Category:** ruby-talk\
**Created:** [16 October 2007 20:31 UTC](https://rubytalk.org/t/how-get-filename-from-ftp-list/41437 "2007-10-16T20:31:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bazsl](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@Bazsl](https://rubytalk.org/u/Bazsl)\
**Post date:** [16 October 2007 20:31 UTC](https://rubytalk.org/t/how-get-filename-from-ftp-list/41437/1 "2007-10-16T20:31:23Z")

</div>

The following code displays the file's date, time size and name. How can I access just the file's name from within the block?

&nbsp;&nbsp;&nbsp;&nbsp;@@ftp.list(file\_template) do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts [f.name](http://f.name)  
&nbsp;&nbsp;&nbsp;&nbsp;end

Thanks.

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [16 October 2007 23:38 UTC](https://rubytalk.org/t/how-get-filename-from-ftp-list/41437/2 "2007-10-16T23:38:31Z")

</div>

Bazsl wrote:

> The following code displays the file's date, time size and name. How can I access just the file's name from within the block?
> 
> &nbsp;&nbsp;&nbsp;@@ftp.list(file\_template) do |f|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts f.name  
> &nbsp;&nbsp;&nbsp;end
> 
> Thanks.

You can use Net:FTP#nlist[1] if you don't need to filter them.  
Otherwise I suppose you could do something like:

@@ftp.list(file\_template) do |f|  
&nbsp;&nbsp;&nbsp;puts f.match(/:\d\d\s(.\*)$/)[1]  
end

Which works for me. I have no idea if FTP servers vary in their ls output.

-Justin

[1][http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/classes/Net/FTP.html#M001055](http://ruby-doc.org/stdlib/libdoc/net/ftp/rdoc/classes/Net/FTP.html#M001055)
