A messy Downloads folder with hundreds of unsorted files is a common problem. This drill teaches you to automatically organize files into categorized subdirectories based on their extensions. You'll learn file operations, directory creation, and how to handle naming conflicts when files already exist.
Use a hash to map categories to arrays of extensions: {'Images' => ['.jpg', '.png'], ...}
Dir.glob with File.file? filters out directories
File.extname returns the extension with the dot, use .downcase for case-insensitive matching
FileUtils.mkdir_p creates directories only if they don't exist
To handle duplicates, check File.exist? and append _1, _2, etc. to the basename
File.basename(path, ext) gives filename without extension
organize_downloads
Moved: archive.zip -> Archives/archive.zip Moved: notes.txt -> Documents/notes.txt Moved: photo.png -> Images/photo.png Moved: report.pdf -> Documents/report.pdf Moved: song.mp3 -> Audio/song.mp3 Moved: vacation.jpg -> Images/vacation.jpg Moved: video.mp4 -> Videos/video.mp4
organize_downloads
puts '---'
puts Dir.exist?('downloads/Images')
puts Dir.exist?('downloads/Documents')
puts Dir.exist?('downloads/Audio')
puts Dir.exist?('downloads/Videos')
puts Dir.exist?('downloads/Archives')
Moved: archive.zip -> Archives/archive.zip Moved: notes.txt -> Documents/notes.txt Moved: photo.png -> Images/photo.png Moved: report.pdf -> Documents/report.pdf Moved: song.mp3 -> Audio/song.mp3 Moved: vacation.jpg -> Images/vacation.jpg Moved: video.mp4 -> Videos/video.mp4 --- true true true true true
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept