Created
June 26, 2011 13:38
-
-
Save eduardodx/1047628 to your computer and use it in GitHub Desktop.
Ruby Shuffle Files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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