Alex Rivera | Logout

Running files in a directory recursively using ruby

Asked 2009-03-27T12:06:54.137
11

I'm working on script right now which has to run each ruby script in a directory and its subfolders.

e.g.

run-all.rb
- scripts
  - folder1
    - script1.rb
    - script2.rb
  - folder2
    - script3.rb
    - script4.rb

As the server is a Windows server I would normally use a batch file but the head dev insists everything must be done in ruby as some members have Macs and may not understand Windows Batch Files.

As the question may have given away, my knowledge of Ruby is very basic.

Edit
Report

1 Answer

1

Something like this should probably work:

def process_directory(basedir)
puts basedir
Find.find(basedir.chomp) do |path|
    if FileTest.directory?(path)
        if File.basename(path)[0] == ?.
            Find.prune       # Don't look any further into this directory.
        else
            next
        end
    else
        puts path
    end
end
answered 2009-03-27T12:11:11.973

Your Answer