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:
# 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)
selected << candidates[nr]
puts "Adding: " + File.basename(candidates[nr])
cursize += File.size(candidates[nr])
puts "Space Left: " + (sizetomatch – cursize).to_s + " bytes"
selected.each do |f|
File.copy(f,finalpath + rand(selected.length).to_s + "-" + File.basename(f))
puts "Copying " + f + " to selected folder "