How to iterate through every file in one directory using Ruby

There are many ways of doing it the one below is my favorite.

We use Dir.glob – function and we iterate through all directories. This might be useful when you need to remove thousand of .txt files after system recover for instance.

Dir.glob(“**/*.txt”) do |my_text_file| # note one extra “*”
puts “working on: #{my_text_file}…”
end

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s