Return actual files only not directories, using Pathname..

Discussion in 'Ruby' started by Brian Wallace, Feb 5, 2009.

  1. [Note: parts of this message were removed to make it a legal post.]

    I'm very new to Ruby, but I'm learning pretty quickly ..What i'm trying to
    do is fairly simple...

    I'd like to use Pathname, to return only actual filenames in the array...
    For instance , using FileList.. You can just do this:

    FileList["*.*"]

    And it will return a string array with the filenames without directories..

    But I rather like Pathname ... Since I can use it like this:

    a = Pathname("/Path")
    b = a.children

    puts b[1] #==> "/Path/file1.txt"

    b[1].read #==> "This is data in a file named file1.txt"
    puts b[2] #==> "/Path/file2.txt"
    .....

    Does anyone have any tips on how I could use Pathname to return files only,
    and not directories?

    Thanks,

    Brian
     
    Brian Wallace, Feb 5, 2009
    #1
    1. Advertising

  2. Brian Wallace

    7stud -- Guest

    Re: Return actual files only not directories, using Pathname

    Brian Wallace wrote:

    > I'd like to use Pathname, to return only actual filenames in the
    > array...
    > For instance , using FileList.. You can just do this:
    >
    > FileList["*.*"]
    >


    FileList? I can't find anything in the ruby docs about FileList.
    google doesn't help either.


    > Does anyone have any tips on how I could use Pathname to return files
    > only,
    > and not directories?


    Just separate out the files after you retrieve all the file names:

    require "pathname"

    pn_obj = Pathname.new("./")
    file_objs = pn_obj.children

    dirs = []
    files = []

    file_objs.each do |obj|
    if obj.directory?
    dirs << obj
    else
    files << obj
    end
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 5, 2009
    #2
    1. Advertising

  3. On Thu, Feb 5, 2009 at 1:50 AM, Brian Wallace <> wrote:

    > Does anyone have any tips on how I could use Pathname to return files only,
    > and not directories?


    Use Pathname.glob

    >> require "pathname"

    => true
    >> Pathname.glob("lib/**/*.rb")

    => [#<Pathname:lib/prawn/compatibility.rb>,
    #<Pathname:lib/prawn/document/annotations.rb>,
    #<Pathname:lib/prawn/document/bounding_box.rb>,
    #<Pathname:lib/prawn/document/destinations.rb>,
    #<Pathname:lib/prawn/document/internals.rb>,
    #<Pathname:lib/prawn/document/page_geometry.rb>,
    #<Pathname:lib/prawn/document/span.rb>,
    #<Pathname:lib/prawn/document/text/box.rb>,
    #<Pathname:lib/prawn/document/text/wrapping.rb>,
    #<Pathname:lib/prawn/document/text.rb>,
    #<Pathname:lib/prawn/document.rb>, #<Pathname:lib/prawn/encoding.rb>,
    #<Pathname:lib/prawn/errors.rb>, #<Pathname:lib/prawn/font/afm.rb>,
    #<Pathname:lib/prawn/font/dfont.rb>,
    #<Pathname:lib/prawn/font/ttf.rb>, #<Pathname:lib/prawn/font.rb>,
    #<Pathname:lib/prawn/graphics/color.rb>,
    #<Pathname:lib/prawn/graphics.rb>,
    #<Pathname:lib/prawn/images/jpg.rb>,
    #<Pathname:lib/prawn/images/png.rb>, #<Pathname:lib/prawn/images.rb>,
    #<Pathname:lib/prawn/literal_string.rb>,
    #<Pathname:lib/prawn/measurement_extensions.rb>,
    #<Pathname:lib/prawn/measurements.rb>,
    #<Pathname:lib/prawn/name_tree.rb>,
    #<Pathname:lib/prawn/pdf_object.rb>,
    #<Pathname:lib/prawn/reference.rb>, #<Pathname:lib/prawn.rb>]



    --
    Technical Blaag at: http://blog.majesticseacreature.com
    Non-tech stuff at: http://metametta.blogspot.com
    "Ruby Best Practices" Book now in O'Reilly Roughcuts:
    http://rubybestpractices.com
     
    Gregory Brown, Feb 5, 2009
    #3
  4. Brian Wallace

    Peña, Botp Guest

    From: Brian Wallace [mailto:]=20
    # I'd like to use Pathname,=20

    Pathname is a very nice fa=E7ade for a lot of file things ;)

    # to return only actual filenames in the array...
    # But I rather like Pathname ... Since I can use it like this:
    # a =3D Pathname("/Path")
    # b =3D a.children
    # puts b[1] #=3D=3D> "/Path/file1.txt"
    # b[1].read #=3D=3D> "This is data in a file named file1.txt"
    # puts b[2] #=3D=3D> "/Path/file2.txt"
    # .....

    try passing a boolean arg to children, eg

    b =3D a.children(false)



    # Does anyone have any tips on how I could use Pathname to=20
    # return files only, and not directories?

    *nixism, they're simply all files, you'll have ask more.

    try #file? or #directory?, eg

    b[1].file?

    b[1].directory?


    warning: be careful when mixing result of #children(false) with other =
    pathname methods.

    on your case, to get only the files (non-dir ie), i'd probably just do,

    b =3D a.children.select do |path|
    path.file?
    end

    and to get a list of the names (as strings ie)

    b.map do |path|=20
    path.basename.to_s=20
    end


    and if you want to get a separate list of files and dirs, try partition, =
    eg

    files, dirs =3D a.children.partition do |path|
    path.file?
    end
     
    Peña, Botp, Feb 5, 2009
    #4
  5. Brian Wallace

    Sascha Abel Guest

    Gregory Brown wrote:
    > On Thu, Feb 5, 2009 at 1:50 AM, Brian Wallace <>
    > wrote:
    >
    >> Does anyone have any tips on how I could use Pathname to return files only,
    >> and not directories?

    >
    > Use Pathname.glob
    >
    >>> require "pathname"

    > => true
    >>> Pathname.glob("lib/**/*.rb")


    You will have to be cautious with this one though, since a directory
    name may contain dots such as 'project.rb'. I guess it's highly unusual
    but I have seen this before ...
    --
    Posted via http://www.ruby-forum.com/.
     
    Sascha Abel, Feb 5, 2009
    #5
  6. Brian Wallace

    James Gray Guest

    Re: Return actual files only not directories, using Pathname

    On Feb 5, 2009, at 1:58 AM, 7stud -- wrote:

    > Brian Wallace wrote:
    >
    >> I'd like to use Pathname, to return only actual filenames in the
    >> array...
    >> For instance , using FileList.. You can just do this:
    >>
    >> FileList["*.*"]
    >>

    >
    > FileList? I can't find anything in the ruby docs about FileList.
    > google doesn't help either.


    It's part of Rake.

    James Edward Gray II
     
    James Gray, Feb 5, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffry van de Vuurst
    Replies:
    2
    Views:
    552
    Jeffry van de Vuurst
    Jul 30, 2003
  2. Black Baptist
    Replies:
    5
    Views:
    2,340
    Black Baptist
    Jan 14, 2005
  3. Simon Harrison

    Pathname: moving files & directories

    Simon Harrison, Apr 6, 2011, in forum: Ruby
    Replies:
    3
    Views:
    116
    Simon Harrison
    Apr 6, 2011
  4. Adam Petrie
    Replies:
    8
    Views:
    333
    Adam Petrie
    Oct 11, 2004
  5. Michael-John Tavantzis

    Return only the directories from readdir

    Michael-John Tavantzis, Mar 7, 2013, in forum: Perl Misc
    Replies:
    5
    Views:
    265
    Alan Curry
    Mar 7, 2013
Loading...

Share This Page