Challenge

Problem

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.

Difficulty: Beginner

Instructions

  1. Scan the downloads/ directory for all files
  2. Categorize files by extension into: Images (.jpg, .jpeg, .png, .gif), Documents (.pdf, .docx, .txt), Videos (.mp4, .avi, .mov), Audio (.mp3, .wav, .flac), Archives (.zip, .tar, .gz)
  3. Create category subdirectories if they don't exist
  4. Move files to appropriate subdirectories
  5. Output 'Moved: filename.ext -> Category/filename.ext' for each file
  6. Sort output alphabetically by original filename

Files

Editable
Read-only

Hints

Hint 1

Use a hash to map categories to arrays of extensions: {'Images' => ['.jpg', '.png'], ...}

Hint 2

Dir.glob with File.file? filters out directories

Hint 3

File.extname returns the extension with the dot, use .downcase for case-insensitive matching

Hint 4

FileUtils.mkdir_p creates directories only if they don't exist

Hint 5

To handle duplicates, check File.exist? and append _1, _2, etc. to the basename

Hint 6

File.basename(path, ext) gives filename without extension

Ruby 3.4

Provided Files (Read-only)

1. Organizes all files into correct categories

Input:
organize_downloads
Expected Output:
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

2. Creates category directories

Input:
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')
Expected Output:
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
+ 2 hidden test cases