After almost a couple of months i have been now able to write a program to lists the files in a directory recursively. I was disappointed at first that i could write a program to do that but now i can. I have also picked up a few useful things on the way to solving the challenge that i would not have understood if i left the challenge or problem. The program is just about ten lines of code in ruby.
1 def list(file_name)
2 Dir.new(“#{file_name}”).each do |file|
3 next if file.match(/^\.+/)
4 path = “#{file_name}/#{file}”
5 if FileTest.directory?(“#{path}”)
6 list(“#{path}”)
7 else
8 puts path
9 end
10 end
11 end
12 list(“/home/foo”)
On line #3, if the directory contains the .. and . directorys move to the next item in loop

2 comments
Comments feed for this article
July 21, 2008 at 7:02 am
Korny
You might also want to check the Ruby ‘find’ standard library
You can get the above down to:
require ‘find’
def list(file_name)
Find.find(file_name) do |path|
puts path unless FileTest.directory?(path)
end
end
September 5, 2008 at 8:17 pm
Whatcha McCallum
try
p Dir['**/*.*']