Skip to content

Instantly share code, notes, and snippets.

@onnimonni
Created May 14, 2015 17:52
Show Gist options
  • Save onnimonni/308739869b34e4812961 to your computer and use it in GitHub Desktop.
Save onnimonni/308739869b34e4812961 to your computer and use it in GitHub Desktop.
File organizer for large amount of images
#!/usr/bin/env ruby
require 'fileutils'
def createNewFolder
picture_dir = ''
loop do
# Picture organizer for Misa
animals = %w(lion tiger lama donkey lizard swordfish elephant rhyno)
types = %w(flamboyant colorful gay obese albino quiet flammable treacherous)
#do
dir_name = "#{types.sample.capitalize}#{animals.sample.capitalize}_#{rand(10000)}"
puts "I chose '#{dir_name}' for directory name."
picture_dir = File.join(Dir.pwd,dir_name)
break unless File.exists? File.join(picture_dir)
end
FileUtils.touch picture_dir
picture_dir
end
def organizeFiles(path)
# Loop all files and organize them into correct folders
Dir.entries(path).each do |file|
next if file == "." or file == ".."
# Filename is like IMG_3483748.png
# We want to order all with the prefix 'IMG'
# Mysterious regex :O
prefix = file.scan(/(^.*)_[0-9]+/i)
unless prefix.empty?
puts "prefix: #{prefix.first.first}"
# Create directory if it doesn't exist
FileUtils.mkdir File.join(path,prefix) unless File.directory? File.join(path,prefix)
# Move original file
FileUtils.mv File.join(path,file), File.join(path,prefix,file)
end
end
end
#1000.times do
# createNewFolder
#end
organizeFiles(Dir.pwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment