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 modification timestamps, 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 all .jpg files from the photos/ directory
  2. For each file, get its modification timestamp (mtime)
  3. Rename the file to 'YYYY-MM-DD_HHMMSS.jpg' format based on that timestamp
  4. Preserve the file extension (handle both .jpg and .jpeg)
  5. Output a summary line for each renamed file: 'Renamed: IMG_001.jpg -> 2024-03-15_143022.jpg'
  6. Sort the output alphabetically by original filename

Files

Editable
Read-only

Hints

Hint 1

Use Dir.glob with pattern 'photos/*.{jpg,jpeg}' to match both extensions

Hint 2

File.mtime returns a Time object with the modification timestamp

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

Sort the original filenames before processing to ensure consistent output order

Provided Files (Read-only)

1. Basic case - 3 photos renamed

Input:
rename_photos_by_date
Expected Output:
Renamed: IMG_001.jpg -> 2024-03-15_143022.jpg
Renamed: IMG_002.jpg -> 2024-03-15_143045.jpg
Renamed: IMG_003.jpg -> 2024-03-15_143110.jpg

2. Single photo

Input:
rename_photos_by_date
Expected Output:
Renamed: IMG_999.jpg -> 2024-03-15_120000.jpg

3. Mixed extensions - .jpg and .jpeg

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
+ 2 hidden test cases