i need a recursive function to list files in a folder:
def procdir(dirname)
data = ''
Dir.foreach(dirname) do |dir|
dirpath = dirname + '/' + dir
if File.directory?(dirpath) then
if dir != '.' && dir != '..' then
#puts "DIRECTORY: #{dirpath}" ;
procdir(dirpath)
end
else
data += dirpath
end
end
return data
end
but the result: is null
Stdlib Dir#glob
recurses when you give it the **
glob.
def procdir(dir)
Dir[ File.join(dir, '**', '*') ].reject { |p| File.directory? p }
end
Use the find
module:
require 'find'
pathes = []
Find.find('.') do |path|
pathes << path unless FileTest.directory?(path)
end
puts pathes.inspect
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With