Hello everyone
I’m trying to make a “directory chooser” for a form, so that the user
can choose a subdirectory (via dropdown) of a specified root directory
(under public/images) on the server. Sooo… how do I get an array or a
hash of all the subdirectories in a specified directories?
And: how can I then get an array or a hash of all the files that are in
that subdir?
Thanks a lot!
al
On Nov 15, 2006, at 8:28 AM, al schmid wrote:
that subdir?
Try this:
def parse_dir(path)
dirs, files = [], []
Dir.entries(path).each do |p|
dirs << p if File.directory?§
files << p if File.file?§
end
dirs[0…2] = [] # removes ‘.’ and ‘…’ directories (if on UNIX)
[dirs,files]
end
directories, files = parse_dir(some_path)
Call additional times with a new path to get subdirectory information.
cr
Hi
Sorry if this is coming to the list a second time…
I’m new to Ruby on Rails and also new here, so apologies if this is
something obvious I’m missing.
I’m also trying to list the contents of a directory and separate them
into files and dirs. The ruby script I’ve written, as well as the one
here, both work fine when run from the terminal. However, when I try
to use either in Rails, the app only recognizes … and . as directories
and .DS_Store as a file, even though it can list the complete contents
of the directory.
Any thoughts? Thanks for any help.
cheers
dafydd
dafydd wrote:
I’m also trying to list the contents of a directory and separate them
into files and dirs. The ruby script I’ve written, as well as the one
here, both work fine when run from the terminal. However, when I try
to use either in Rails, the app only recognizes … and . as directories
and .DS_Store as a file, even though it can list the complete contents
of the directory.
Perhaps this code snippet will help. Also try logging/outputting the
result of Dir.pwd, i.e. where is your current working directory under
each circumstance?
class Dir
def self.crawl( path, max_depth=nil, include_directories=false,
depth=0, &block )
return if max_depth && depth > max_depth
begin
if File.directory?( path )
yield( path, depth ) if include_directories
files = Dir.entries( path ).select{ |f| f[0,1]!=’.’ }
unless files.empty?
files.collect!{ |file_path|
Dir.crawl( path+’/’+file_path, max_depth,
include_directories, depth+1, &block )
}.flatten!
end
return files
else
yield( path, depth )
end
rescue SystemCallError => the_error
warn “ERROR: #{the_error}”
end
end
end
Hey thanks!
I have to admit the code snippet is a bit beyond my grasp at the
moment, and I’m gonna have to spend some time figuring it out, but in
the meantime I followed your current directory tip and taht fixed the
trouble. As far as I can figure, I needed to actually bbe working in
the directory - it wasn’t enough to just provide the pathname.
Thanks again.
cheers
dafydd
On 12/15/06, Phrogz [email protected] wrote:
result of Dir.pwd, i.e. where is your current working directory under
unless files.empty?
warn “ERROR: #{the_error}”
end
end
end
–
www.sideshowmedia.ca
skype: chickeninthegrass