I want to get the names of all files in a given directory. When I employ the Dir[] method, it returns the name of the files with the pathname I invoked it with: allfiles = Dir["public/images/icons/**"] So to remedy this, I try to sub out the directory prefixing the string, as: for fil in allfiles do render :text => fil.sub( "public/images/icons/", "" ) end Yet even this doesn''t remove the directory name. Clearly I am doing something stooopid, but just don''t see it. Can someone please have a look and see what I am missing here? Thanks you, Janna
On May 7, 2009, at 2:06 PM, JannaB wrote:> > I want to get the names of all files in a given directory. When I > employ the Dir[] method, it returns the name of the files with the > pathname I invoked it with: > > allfiles = Dir["public/images/icons/**"] > > So to remedy this, I try to sub out the directory prefixing the > string, as: > > for fil in allfiles do > render :text => fil.sub( "public/images/icons/", > "" ) > end > > Yet even this doesn''t remove the directory name. Clearly I am doing > something stooopid, but just don''t see it. Can someone please have a > look and see what I am missing here? Thanks you, JannaFile.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb" File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
Dir.entries("public/images/icons") should do nicely... On May 7, 11:06 am, JannaB <mistressja...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> I want to get the names of all files in a given directory. When I > employ the Dir[] method, it returns the name of the files with the > pathname I invoked it with: > > allfiles = Dir["public/images/icons/**"] > > So to remedy this, I try to sub out the directory prefixing the > string, as: > > for fil in allfiles do > render :text => fil.sub( "public/images/icons/", > "" ) > end > > Yet even this doesn''t remove the directory name. Clearly I am doing > something stooopid, but just don''t see it. Can someone please have a > look and see what I am missing here? Thanks you, Janna
Rick Lloyd wrote:> Dir.entries("public/images/icons") should do nicely...And in any case: fil = "public/images/icons/hello.ico" p fil.sub("public/images/icons/", "") --output:-- "hello.ico" -- Posted via http://www.ruby-forum.com/.
JannaB wrote:> I want to get the names of all files in a given directory. When I > employ the Dir[] method, it returns the name of the files with the > pathname I invoked it with: > > allfiles = Dir["public/images/icons/**"] > > So to remedy this, I try to sub out the directory prefixing the > string, as: > > for fil in allfiles do > render :text => fil.sub( "public/images/icons/", > "" ) > end > > Yet even this doesn''t remove the directory name. Clearly I am doing > something stooopid, but just don''t see it. Can someone please have a > look and see what I am missing here? Thanks you, Jannarequire ''pathname'' allfiles = Pathname.new(''/opt'') allfiles.children.map{|a| a.basename.to_s} or Dir.chdir("/opt") do allfiles = Dir["**"] end -- Posted via http://www.ruby-forum.com/.