Running files in a directory recursively using ruby

Macha picture Macha · Mar 27, 2009 · Viewed 6.9k times · Source

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.

Answer

pjb3 picture pjb3 · Mar 27, 2009

Depends what you mean by "run". To just execute the code that is in each script within the same ruby process, this will do the trick:

Dir["scripts/**/*.rb"].each{|s| load s }

But it you want to run each script in it's own ruby process, then try this:

Dir["scripts/**/*.rb"].each{|s| puts `ruby #{s}` }

Just put the either of these in the contents of run-all.rb and the run ruby run-all.rb form the command line.