challenge #4, listing files recursively with Ruby
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
About this entry
You’re currently reading “challenge #4, listing files recursively with Ruby,” an entry on Connecting the dots
- Published:
- 2.16.08 / 2am
- Category:
- challenges
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]