CD-o-matic with Ruby

With just a bunch of ruby lines I created a small utility that fills a folder with songs picked randomly from a choosen repository until the maximum specified size is reached.

This tool is very useful for me ‘cause I can change my USB player music day-by-day without choosing each song from my repository (which is a boring and extremely long operation).

So, here you are the code:

require "ftools"

# Path were you want to store the randomly picked music

finalpath = "/Users/sandro/mp3xcd/"

# Path from which pick the music

repository = "/Volumes/ntfs/Musica/"

# Size Limit for the ‘finalpath’ folder (in bytes)

sizetomatch = 650000000

selected = []

cursize = 0

candidates = Dir.glob(repository+"**/*.mp3")

while cursize < sizetomatch do

nr = rand(candidates.length)

unless selected.include?(candidates[nr])

selected << candidates[nr]

puts "Adding: " + File.basename(candidates[nr])

cursize += File.size(candidates[nr])

puts "Space Left: " + (sizetomatch – cursize).to_s + " bytes"

end

end

selected.each do |f|

File.copy(f,finalpath + rand(selected.length).to_s + "-" + File.basename(f))

puts "Copying " + f + " to selected folder "

end

Leave a Comment

*