Skip to content

Instantly share code, notes, and snippets.

@Kequc
Last active January 15, 2018 09:15
Show Gist options
  • Save Kequc/8740833 to your computer and use it in GitHub Desktop.
Save Kequc/8740833 to your computer and use it in GitHub Desktop.
Rename directory of files in random order to simple numerical form
require 'fileutils'
# Set folder path
folder_path = "/User/absolute/path/to/dir"
# Start at number
start = 0
# Group into directories of size
size = 125
# "%07d" for 7 digits ie 0000001.jpg
padding = "%07d"
# Then run this monster
Dir.glob("#{folder_path}/*").shuffle.each_with_index { |f,i| path = folder_path + "/" + (padding % (((i + start) / size).round * size)); FileUtils.mkdir_p(path); File.rename(f, path + "/" + (padding % (i + start)) + File.extname(f)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment