tags:

views:

1020

answers:

3

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

+2  A: 

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)

Michael Sepcot
+1  A: 

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.

glenra
+5  A: 

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

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