Most elegant way: array of files from array of directories

T

Terry Michaels

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
 
J

Jesús Gabriel y Galán

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

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

Robert Klemme

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
 
D

David Masover

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

w_a_x_man

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

dir_paths.map{|d| Dir[ File.join( d, "*") ] }.flatten.
select{|f| test ?f, f}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top