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, .png, .gif), Documents (.pdf, .docx, .txt), Videos (.mp4, .avi), Audio (.mp3, .wav), Archives (.zip, .tar, .gz)
  3. Create category subdirectories if they don't exist
  4. Move files to appropriate subdirectories
  5. Handle duplicate filenames by appending a number: file.pdf -> file_1.pdf
  6. Output a summary: 'Moved: filename.ext -> Documents/filename.ext' for each file
  7. 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

Provided Files (Read-only)

1. Basic case - organize mixed files

Input:
organize_downloads
Expected Output:
Moved: report.pdf -> Documents/report.pdf
Moved: song.mp3 -> Audio/song.mp3
Moved: vacation.jpg -> Images/vacation.jpg

2. Multiple file types

Input:
organize_downloads
Expected Output:
Moved: archive.zip -> Archives/archive.zip
Moved: notes.txt -> Documents/notes.txt
Moved: photo.png -> Images/photo.png
Moved: video.mp4 -> Videos/video.mp4

3. Duplicate filename handling

Input:
organize_downloads
Expected Output:
Moved: document.pdf -> Documents/document_1.pdf
+ 2 hidden test cases