Last active
February 9, 2024 17:07
-
-
Save puppe1990/4437fbe3a678311e63432e459041a071 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'fileutils' | |
# Define folder names and corresponding file formats | |
folders = { | |
'music' => ['.mp3', '.wav', '.flac', '.m4a', '.aac'], | |
'images' => ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.JPG', '.PNG'], | |
'photos' => ['.cr2', '.raw', '.nef', '.dng', '.orf'], | |
'web_files' => ['.axd', '.css', '.HTM', '.html', '.js', '.json', '.svg'], | |
'documents' => ['.csv', '.doc', '.docx', '.epub', '.odt', '.ofx', '.pdf', '.PDF', '.pfx', '.potx', '.txt', '.vcf', | |
'.xcf', '.xls', '.xlsx'], | |
'compressed_files' => ['.gz', '.tgz', '.zip', '.zpl'], | |
'executables' => ['.deb', '.exe', '.flatpakref', '.msi'], | |
'videos' => ['.mov', '.MOV', '.mp4', '.MP4'], | |
'other_files' => ['.iso', '.rb', '.webp'] | |
} | |
# Check if folders exist, if not, create them | |
folders.keys.each do |folder| | |
FileUtils.mkdir_p(folder) unless File.directory?(folder) | |
end | |
# Iterate through files in current directory | |
Dir.entries('.').each do |file| | |
# Skip directories | |
next if File.directory?(file) | |
# Initialize a flag to check if the file extension is found | |
extension_found = false | |
# Check file extension and move to corresponding folder | |
folders.each do |folder, extensions| | |
if extensions.include?(File.extname(file).downcase) | |
FileUtils.mv(file, "#{folder}/#{file}") | |
extension_found = true | |
break | |
end | |
end | |
# If the file extension was not found, create a new folder for it | |
unless extension_found | |
ext = File.extname(file).downcase | |
ext = 'unknown' if ext.empty? # Handle files without an extension | |
new_folder = "other_extensions/#{ext}" | |
FileUtils.mkdir_p(new_folder) unless File.directory?(new_folder) | |
FileUtils.mv(file, "#{new_folder}/#{file}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment