Tuesday, May 3, 2011

get all of the immediate subdirectories in ruby

I'm trying to write a simple ruby script that will copy a index.tpl to index.html in all of the subdirectories (with a few exceptions). But I'm getting bogged down by trying to get the list of subdirectories

From stackoverflow
  • Dir.glob("**/") will return an array of all paths underneath the current directory. From there you can filter the list and copy a file with File.copy(from, to)

  • If you mean to find all the immediate subdirectories (just one level below where you are), try this:

    Dir.chdir("/some/path/you/want/to/check/below")
    subdir_list=Dir["*"].reject{|o| not File.directory?(o)}
    

    That is: change directory someplace, construct an array of files found there, reject those array elements that aren't directories, and return the resulting culled arrray.

  • Assuming you only wanted the immediate subdirectories, you could use Dir['*/'] (which combines Micheal Sepcot's and glenra's answers).

    Andrew Bullock : whats to "assume"? thats what he asked in the question! +1

0 comments:

Post a Comment