Challenge

Problem

IT administrators, compliance teams, and migration planners need detailed file inventories before system changes. This drill teaches you to scan directory trees and generate CSV reports with comprehensive file metadata. You'll learn file metadata extraction using a metadata file (simulating real file stats), data aggregation, and report formatting. Note: This drill parses CSV manually using string methods rather than Ruby's CSV library.

Difficulty: Beginner

Instructions

  1. Load file metadata from 'files/metadata.json' (contains size_bytes and mtime for each file path)
  2. Collect metadata for each file: filename, full path, size (MB), modification date, file type (extension)
  3. Sort files by size (largest first)
  4. Generate CSV with headers: Filename,Path,Size_MB,Modified,Type
  5. Print summary statistics:
    • Total files: X
    • Total size: Y.YY MB
    • File types breakdown: .ext (count), ...
  6. Return the CSV content as a string

Files

Editable
Read-only

Hints

Hint 1

Load metadata with JSON.parse(File.read('files/metadata.json'))

Hint 2

Use metadata.map to iterate over each file path and its info

Hint 3

Calculate MB: (info['size_bytes'] / 1048576.0).round(2)

Hint 4

Build CSV manually: lines << "#{val1},#{val2},#{val3}"

Hint 5

Sort descending by size: sort_by! { |f| -f[:size_mb] }

Hint 6

Group by type with: group_by { |f| f[:type] }.transform_values(&:count)

Ruby 3.4

Provided Files (Read-only)

1. Generates correct CSV headers

Input:
csv = generate_inventory
puts csv.lines.first.strip
Expected Output:
Total files: 4
Total size: 0.90 MB
File types: .pdf (1), .jpg (1), .csv (1), .txt (1)
Filename,Path,Size_MB,Modified,Type

2. Files sorted by size (largest first)

Input:
csv = generate_inventory
data_lines = csv.lines[1..4]
puts data_lines.map { |l| l.split(',')[0] }.join(', ')
Expected Output:
Total files: 4
Total size: 0.90 MB
File types: .pdf (1), .jpg (1), .csv (1), .txt (1)
report.pdf, photo.jpg, data.csv, notes.txt

3. Correct total files

Input:
generate_inventory
Expected Output:
Total files: 4
Total size: 0.90 MB
File types: .pdf (1), .jpg (1), .csv (1), .txt (1)
+ 2 hidden test cases