Skip to content

Instantly share code, notes, and snippets.

@alex-quiterio
Created September 15, 2024 18:09
Show Gist options
  • Select an option

  • Save alex-quiterio/de6e046f93dccc15779d85a5ab8896bf to your computer and use it in GitHub Desktop.

Select an option

Save alex-quiterio/de6e046f93dccc15779d85a5ab8896bf to your computer and use it in GitHub Desktop.
require 'fileutils'
# Define file type extensions
IMAGE_EXTENSIONS = %w[.jpg .jpeg .png .gif .bmp .tiff]
PDF_EXTENSIONS = %w[.pdf]
AUDIO_EXTENSIONS = %w[.mp3 .wav .ogg .aac .flac]
VIDEO_EXTENSIONS = %w[.mp4 .mkv .mov .avi .wmv]
ORIGIN_FOLDER = "/Users/alex-quiterio/Desktop/triage"
DESTINATION_FOLDERS = {
# soft: "~/gdrive-remote-folder",
soft: "/Users/alex-quiterio/Downloads",
heavy: "/Users/alex-quiterio/Downloads"
}
# Function to move files based on type
def organize_files(dry_run = false)
# Create directories for each type in the destination folder if they don't exist
images_folder = File.join(DESTINATION_FOLDERS[:soft], "__triage-images")
pdfs_folder = File.join(DESTINATION_FOLDERS[:soft], "__triage-pdfs")
audios_folder = File.join(DESTINATION_FOLDERS[:heavy], "__triage-audios")
videos_folder = File.join(DESTINATION_FOLDERS[:heavy], "__triage-videos")
FileUtils.mkdir_p(images_folder)
FileUtils.mkdir_p(pdfs_folder)
FileUtils.mkdir_p(audios_folder)
FileUtils.mkdir_p(videos_folder)
# Iterate over files in the origin folder
Dir.foreach(ORIGIN_FOLDER) do |file|
next if File.directory?(file) # Skip directories
file_path = File.join(ORIGIN_FOLDER, file)
file_extension = File.extname(file).downcase
# Move the file to the respective folder in the destination
case file_extension
when *IMAGE_EXTENSIONS
puts "move-file:#{file}=>#{images_folder}" if dry_run
FileUtils.mv(file_path, images_folder) unless dry_run
when *PDF_EXTENSIONS
puts "move-file:#{file}=>#{pdfs_folder}" if dry_run
FileUtils.mv(file_path, pdfs_folder) unless dry_run
when *AUDIO_EXTENSIONS
puts "move-file:#{file}=>#{audios_folder}" if dry_run
FileUtils.mv(file_path, audios_folder) unless dry_run
when *VIDEO_EXTENSIONS
puts "move-file:#{file}=>#{videos_folder}" if dry_run
FileUtils.mv(file_path, videos_folder) unless dry_run
end
end
puts "Files have been triaged successfully!"
end
dry_run = ARGV[0]
if !Dir.exist?(ORIGIN_FOLDER)
puts "The origin folder does not exist."
FileUtils.mkdir_p(ORIGIN_FOLDER)
end
if !Dir.exist?(DESTINATION_FOLDERS[:soft])
puts "The destination soft foles folder does not exist. Creating it..."
FileUtils.mkdir_p(DESTINATION_FOLDERS[:soft])
end
if !Dir.exist?(DESTINATION_FOLDERS[:heavy])
puts "The destination heavy foles folder does not exist. Creating it..."
FileUtils.mkdir_p(DESTINATION_FOLDERS[:heavy])
end
organize_files(dry_run == "--dry-run")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment