Traversing Directories with Ruby

written by Jerod on July 15th, 2008 @ 03:32 PM

If you want to shove filenames of all files in a directory into an array, do:

   1  # (absolute path)
   2  files = Dir["/Users/jerod/src/**"]
   3  # (relative path)
   4  files = Dir[File.expand_path("~/src") + "/**"]
   5  # (in ENV["PWD"], aka current directory)
   6  files = Dir["**"]

If you want to shove filenames of all files in a directory recursively into an array, do:

   1  # (absolute path)
   2  files = Dir["/Users/jerod/src/**/**"]
   3  # (relative path)
   4  files = Dir[File.expand_path("~/src") + "/**/**"]
   5  # (in ENV["PWD"], aka current directory)
   6  files = Dir["**/**"]

It can’t get much easier than that.

Post a comment

Options:

Size

Colors

Clicky Web Analytics