Challenge

Problem

Photographers often end up with hundreds of files named IMG_001.jpg, IMG_002.jpg, etc. This drill teaches you to rename files based on their capture timestamps from a metadata file, making them searchable and sortable. You'll use Ruby's file operations and date formatting to transform meaningless camera filenames into organized, dated filenames.

Difficulty: Beginner

Instructions

  1. Read the metadata.json file which contains photo filenames and their capture timestamps
  2. For each photo in the metadata, rename it to 'YYYY-MM-DD_HHMMSS' format based on its timestamp
  3. Preserve the original file extension
  4. Output a summary line for each renamed file: 'Renamed: [old_name] -> [new_name]'
  5. Sort the output alphabetically by original filename

Files

Editable
Read-only

Hints

Hint 1

Read the metadata.json file using File.read and parse it with JSON.parse

Hint 2

Use Time.parse to convert the ISO timestamp string into a Time object

Hint 3

Time#strftime('%Y-%m-%d_%H%M%S') formats the date correctly

Hint 4

File.extname gives you the extension including the dot

Hint 5

File.rename(old_path, new_path) moves/renames the file

Hint 6

Collect results into an array, sort it, then output each line

Ruby 3.4

Provided Files (Read-only)

1. Renames all photos with correct timestamps

Input:
rename_photos_by_date
Expected Output:
Renamed: DSC_100.jpeg -> 2024-03-15_143100.jpeg
Renamed: IMG_001.jpg -> 2024-03-15_143022.jpg
Renamed: IMG_002.jpg -> 2024-03-15_143045.jpg

2. Output is sorted alphabetically by original filename

Input:
rename_photos_by_date
puts '---'
Dir.glob('photos/*.{jpg,jpeg}').sort.each { |f| puts File.basename(f) }
Expected Output:
Renamed: DSC_100.jpeg -> 2024-03-15_143100.jpeg
Renamed: IMG_001.jpg -> 2024-03-15_143022.jpg
Renamed: IMG_002.jpg -> 2024-03-15_143045.jpg
---
2024-03-15_143022.jpg
2024-03-15_143045.jpg
2024-03-15_143100.jpeg
+ 2 hidden test cases