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.
Use Dir.glob with pattern 'photos/*.{jpg,jpeg}' to match both extensions
File.mtime returns a Time object with the modification timestamp
Time#strftime('%Y-%m-%d_%H%M%S') formats the date correctly
File.extname gives you the extension including the dot
File.rename(old_path, new_path) moves/renames the file
Sort the original filenames before processing to ensure consistent output order
rename_photos_by_date
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
rename_photos_by_date
Renamed: IMG_999.jpg -> 2024-03-15_120000.jpg
rename_photos_by_date
Renamed: DSC_100.jpeg -> 2024-03-15_143100.jpeg Renamed: IMG_001.jpg -> 2024-03-15_143022.jpg
Console output will appear here...
Are you sure?
You're making great progress