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.
Read the metadata.json file using File.read and parse it with JSON.parse
Use Time.parse to convert the ISO timestamp string into a Time object
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
Collect results into an array, sort it, then output each line
rename_photos_by_date
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
rename_photos_by_date
puts '---'
Dir.glob('photos/*.{jpg,jpeg}').sort.each { |f| puts File.basename(f) }
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
Console output will appear here...
Are you sure?
You're making great progress
Become a Ruby Pro
1,600+ problems to master every concept