Skip to content

Instantly share code, notes, and snippets.

@montethinks
Created November 9, 2021 19:30
Show Gist options
  • Save montethinks/f9c97079eef5a64fe7757a16bf862bb2 to your computer and use it in GitHub Desktop.
Save montethinks/f9c97079eef5a64fe7757a16bf862bb2 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'fileutils'
home = -> { Dir.chdir(Dir.home) }
downloads = -> { Dir.chdir('Downloads') }
home.call
downloads.call
FOLDERS = %w[PDF ZIP PHOTOS CSV VIDEOS].freeze
def current
Dir.new(Dir.pwd)
end
def directories
current.children.select { |f| File.directory?(f) }
end
def setup
FOLDERS.each do |f|
!directories.include?(f) ? Dir.mkdir(f) : nil
end
end
def all_files
current.children.select { |f| File.file?(f) }
end
def sort_files
base_path = "#{Dir.pwd}"
all_files.each do |file|
# TODO: refactor into hashes
# http://bobnadler.com/articles/2010/08/02/refactoring-case-statements-in-ruby.html
if File.extname(file) == '.pdf'
pdf_path = File.join(base_path, "PDF")
FileUtils.mv(File.join(base_path, file), File.join(pdf_path, file))
puts "PDF moved"
elsif File.extname(file) == '.zip'
zip_path = File.join(base_path, "ZIP")
FileUtils.mv(File.join(base_path, file), File.join(zip_path, file))
puts "ZIPPED"
elsif %w[.gif .png .PNG .jpg].include?(File.extname(file))
photo_path = File.join(base_path, "PHOTOS")
FileUtils.mv(File.join(base_path, file), File.join(photo_path, file))
puts "SOME PHOTO"
elsif %w[.csv .xls .xlsx].include?(File.extname(file))
spreadsheet_path = File.join(base_path, "CSV")
FileUtils.mv(File.join(base_path, file), File.join(spreadsheet_path, file))
puts "SOME SPREADSHEET"
elsif %w[.mov .MOV].include?(File.extname(file))
video_path = File.join(base_path, "VIDEOS")
FileUtils.mv(File.join(base_path, file), File.join(video_path, file))
end
end
end
# want .pdf, .zip, .csv, .json, .png, .jpg
# directories PDF ZIP PHOTOS CSV
puts ".... Starting sort in #{Dir.pwd} .... "
setup
sort_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment