handle ArgumentError to resume loop at next iteration

Discussion in 'Ruby' started by jay, Feb 22, 2007.

  1. jay

    jay Guest

    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

    --
    Posted via http://www.ruby-forum.com/.
    jay, Feb 22, 2007
    #1
    1. Advertising

  2. Austin Ziegler, Feb 22, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Deniz Bahar
    Replies:
    2
    Views:
    463
    Andrey Tarasevich
    Mar 9, 2005
  2. Rudi
    Replies:
    5
    Views:
    4,996
  3. Tad McClellan
    Replies:
    3
    Views:
    138
    Edward Wijaya
    May 13, 2004
  4. Nene
    Replies:
    6
    Views:
    346
    John W. Krahn
    Dec 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page