Home > Ruby > Comparing Directories Ruby Style

Comparing Directories Ruby Style

I recently needed to recursively compare 2 directories by filename. Here’s the solution:

require 'ftools' 
srcDir = "/home/Music/"destDir = "/home/Music/iTunes/iTunes Music"

mHash = Hash.newiHash = Hash.new
 #Add all files to hash Dir[srcDir + '/**/*.mp3'].each do |path|    mHash[path.split('/').last] = pathend

#Add all files to second hash Dir[destDir + "/**/*.mp3"].each do |path|    iHash[path.split('/').last] = pathend

puts "Source # Files: " + mHash.length.to_sputs "Dest # Files: " + iHash.length.to_s

#Compare Hashes and copy Any datam Hash.each_key do |key|     if iHash[key] == nil         puts "Copying " + mHash[key] +" to " + destDir         File.copy(mHash[key],destDir+"/")         end end
Advertisements
Categories: Ruby
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: