Skip to content

Instantly share code, notes, and snippets.

@eduardodx
Created June 26, 2011 13:38
Show Gist options
  • Save eduardodx/1047628 to your computer and use it in GitHub Desktop.
Save eduardodx/1047628 to your computer and use it in GitHub Desktop.
Ruby Shuffle Files
# encoding: utf-8
# Ruby script to shuffle files, pass the folder name as the first argument (necessary),
# and -f after folder name if you want to really sort them
# example: ruby ruby_shuffle_files.rb my_folder -f
folder = ARGV[0] + "/"
loop_times = (ARGV[1] != nil && ARGV[1] == "-f") ? 3 : 1
qtd_files = 0
# Verify if folder isn't empty
raise "Folder can't be empty." if folder == nil
dir = Dir.open(folder)
# Count Files
dir.each do |f|
qtd_files += 1
end
# Loop 3 times if -f is passed or 1 if not
loop_times.times do
# Rename Files
dir.each do |f|
next if f == "." || f == ".."
ext = File.extname(f)
random = ""
# Check if there's not other file with the same name
loop do
random = rand(max=qtd_files).to_s
break if !File.exists? folder + random + ext
end
new_name = random + ext
File.rename(folder + f, folder + new_name)
end
end
require 'digest/md5'
path = 'musics/'
dir = Dir.new(path)
#print "There are #{dir.length} files. \n"
dir.each do |f|
if f != "." and f != ".." then
new_name = (rand * 10000).to_i
new_name = new_name.to_s + Digest::MD5.hexdigest(f)[0, 8] + ".mp3"
File.rename(path + f, path + new_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment