handle ArgumentError to resume loop at next iteration

J

jay

Hi,

still a noob, and still hacking a script to traverse photo directories
to populate a mysql db with various photo metadata. nearly there, but
having trouble handling an error from DateTime that dies on a file that
doesn't have 3 elements of a civil date.
can anyone recommend a way of handling this error so that the loop skips
to the next iteration?
code below.
many thanks in advance

===
....
find.find(directory) do |path|

next if File.directory?(path)
next unless path =~ /.jpg$/ or /.JPG$/

puts "trying picture: #{path} "
# get metadata
pathName = path
baseName = File.basename(path)
fileSize = File.size(path)
cameraModel = EXIFR::JPEG.new(path).exif.model
picWidth = EXIFR::JPEG.new(path).width
picHeight = EXIFR::JPEG.new(path).height
picDateTaken = EXIFR::JPEG.new(path).date_time_original
DateTime.parse("#{picDateTaken}")
^^^error point

mysqlDate = DateTime.parse("#{picDateTaken}")
picShutterSpeed = EXIFR::JPEG.new(path).exposure_time
picAperture = EXIFR::JPEG.new(path).fnumber #returns a fraction as
string
aperture = picAperture.to_f
focalLength = EXIFR::JPEG.new(path).focal_length
num+= 1

# => PRINT
puts "added picture: #{pathName} total pics:#{num}"

db.query("INSERT INTO JPGS (name, path, size, model, width, height,
timeStamp, shutterSpeed, aperture, focalLength)
VALUES ('#{baseName}', '#{pathName}', '#{fileSize}', '#{cameraModel}',
'#{picWidth}', '#{picHeight}', '#{mysqlDate}', '#{picShutterSpeed}',
'#{aperture}','#{focalLength}') ")

end
 
A

Austin Ziegler

[...]
begin
DateTime.parse(picDateTaken)
rescue ArgumentError
next
end
[...]

-austin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top