Most elegant way: array of files from array of directories

Discussion in 'Ruby' started by Terry Michaels, Oct 16, 2010.

  1. I have a array of directories passed into my program. I want a new array
    which is a list of all files in said directories (only one level deep is
    necessary). N.B.:

    1. Each elements of the new array must be an absolute (full) path, or at
    least relative to the cwd. (Assume all directories provided are also
    absolute or relative to cwd)
    2. OS and preference agnostic: Especially, I do not know what directory
    separator will be used (say, backward- or forward-slash) or whether
    there will be a trailing directory separator in the directory path (say,
    a trailing forward-slash).

    Dir.foreach almost does what I want, but gives relative output instead
    of absolute.

    array_of_file_paths = Array.new

    array_of_directory_paths.each do |directory_path|
    # ???
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    Terry Michaels, Oct 16, 2010
    #1
    1. Advertising

  2. On Sat, Oct 16, 2010 at 6:50 AM, Terry Michaels <> wrot=
    e:
    > I have a array of directories passed into my program. I want a new array
    > which is a list of all files in said directories (only one level deep is
    > necessary). N.B.:
    >
    > 1. Each elements of the new array must be an absolute (full) path, or at
    > least relative to the cwd. (Assume all directories provided are also
    > absolute or relative to cwd)
    > 2. OS and preference agnostic: Especially, I do not know what directory
    > separator will be used (say, backward- or forward-slash) or whether
    > there will be a trailing directory separator in the directory path (say,
    > a trailing forward-slash).
    >
    > Dir.foreach almost does what I want, but gives relative output instead
    > of absolute.
    >
    > array_of_file_paths =3D Array.new
    >
    > array_of_directory_paths.each do |directory_path|
    > =A0# ???
    > end
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    Probably not the most elegant way, but this can give you some ideas:

    irb(main):032:0> Dir["*"].each do |f|
    irb(main):033:1* next if test ?f,f # skip the files
    irb(main):034:1> puts Dir["#{f}/*"].map {|x| File.expand_path(x)}
    irb(main):035:1> end

    You can loop around this collecting the subarrays and then flatten.

    Jesus.
     
    Jesús Gabriel y Galán, Oct 16, 2010
    #2
    1. Advertising

  3. On 16.10.2010 06:50, Terry Michaels wrote:
    > I have a array of directories passed into my program. I want a new array
    > which is a list of all files in said directories (only one level deep is
    > necessary). N.B.:
    >
    > 1. Each elements of the new array must be an absolute (full) path, or at
    > least relative to the cwd. (Assume all directories provided are also
    > absolute or relative to cwd)
    > 2. OS and preference agnostic: Especially, I do not know what directory
    > separator will be used (say, backward- or forward-slash) or whether
    > there will be a trailing directory separator in the directory path (say,
    > a trailing forward-slash).


    You can always use a forward slash in Ruby. Duplicate slashes are not
    an issue either IIRC.

    > Dir.foreach almost does what I want, but gives relative output instead
    > of absolute.
    >
    > array_of_file_paths = Array.new
    >
    > array_of_directory_paths.each do |directory_path|
    > # ???
    > end


    How about

    array_of_file_paths = array_of_directory_paths.map do |dir|
    Dir["#{dir}/*", "#{dir}/.*"]
    end.delete_if {|f| test ?d, f}

    ? Of course, if you do not need hidden files you can omit the second
    arg to Dir[].

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Oct 16, 2010
    #3
  4. On Saturday, October 16, 2010 05:00:10 am Robert Klemme wrote:
    > On 16.10.2010 06:50, Terry Michaels wrote:
    > > I have a array of directories passed into my program. I want a new array
    > > which is a list of all files in said directories (only one level deep is
    > > necessary). N.B.:
    > >
    > > 1. Each elements of the new array must be an absolute (full) path, or at
    > > least relative to the cwd. (Assume all directories provided are also
    > > absolute or relative to cwd)
    > > 2. OS and preference agnostic: Especially, I do not know what directory
    > > separator will be used (say, backward- or forward-slash) or whether
    > > there will be a trailing directory separator in the directory path (say,
    > > a trailing forward-slash).

    >
    > You can always use a forward slash in Ruby.


    Also worth mentioning: I don't know of any OSes Ruby supports which don't also
    support a forward Slash. For example, while Windows typically uses
    backslashes, it also supports forward slashes.
     
    David Masover, Oct 16, 2010
    #4
  5. Terry Michaels

    w_a_x_man Guest

    On Oct 15, 11:50 pm, Terry Michaels <> wrote:
    > I have a array of directories passed into my program. I want a new array
    > which is a list of all files in said directories (only one level deep is
    > necessary). N.B.:
    >
    > 1. Each elements of the new array must be an absolute (full) path, or at
    > least relative to the cwd. (Assume all directories provided are also
    > absolute or relative to cwd)
    > 2. OS and preference agnostic: Especially, I do not know what directory
    > separator will be used (say, backward- or forward-slash) or whether
    > there will be a trailing directory separator in the directory path (say,
    > a trailing forward-slash).
    >
    > Dir.foreach almost does what I want, but gives relative output instead
    > of absolute.
    >
    > array_of_file_paths = Array.new
    >
    > array_of_directory_paths.each do |directory_path|
    >   # ???
    > end
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    dir_paths.map{|d| Dir[ File.join( d, "*") ] }.flatten.
    select{|f| test ?f, f}
     
    w_a_x_man, Oct 16, 2010
    #5
    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. Rob Cowie
    Replies:
    28
    Views:
    673
    Bruno Desthuilliers
    Jun 13, 2006
  2. Duk Lee
    Replies:
    2
    Views:
    750
    Alexey Smirnov
    Jul 23, 2007
  3. Alex Buell

    What's the most elegant way of doing this?

    Alex Buell, Feb 3, 2009, in forum: C Programming
    Replies:
    3
    Views:
    348
    Alex Buell
    Feb 4, 2009
  4. Replies:
    32
    Views:
    329
    William James
    Nov 28, 2007
  5. Kenneth McDonald
    Replies:
    2
    Views:
    119
    Kenneth McDonald
    Sep 2, 2008
Loading...

Share This Page