I know this is a nub question, but the documentation on ruby is
terrible.
How do I loop through all of the files in a directory?
Thanks for your help.
I know this is a nub question, but the documentation on ruby is
terrible.
How do I loop through all of the files in a directory?
Thanks for your help.
On 5/4/07, Ben J. [email protected] wrote:
I know this is a nub question, but the documentation on ruby is
terrible.How do I loop through all of the files in a directory?
Thanks for your help.
–
Posted via http://www.ruby-forum.com/.
Have a look at Find.find and Dir.each.
On Sat, 5 May 2007, Ben J. wrote:
I know this is a nub question, but the documentation on ruby is
terrible.How do I loop through all of the files in a directory?
Thanks for your help.
–
Posted via http://www.ruby-forum.com/.
harp:~ > ls dir
a b c
harp:~ > cat a.rb
class Dir
def self.ls dir, glob = File.join(‘', '’), &block
ret = [] unless block
Dir.glob(File.join(dir, glob)) do |entry|
block ? block.call(entry) : ret.push(entry)
end
ret
end
end
Dir.ls ‘dir’ do |entry|
puts entry
end
entries = Dir.ls ‘dir’
p entries
harp:~ > ruby a.rb
dir/a
dir/b
dir/c
[“dir/a”, “dir/b”, “dir/c”]
-a
def GetRecursiveFileList ( dirname )
results = Array.new
# both methods should work the same (I think one returns the
directory
names in addition to the files)
use_method = 0
if use_method == 0
Dir["#{dirname}//"].each do | thisfile |
thisfile.gsub! ( /// , ‘\’ )
results.push ( thisfile )
end
else
require ‘find’
Find.find ( dirname ) do | thisfile |
thisfile.gsub! ( /// , ‘\’ )
results.push ( thisfile )
end
end
return results
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs