# Noob question:traverse directory and populate mysql db

**URL:** <https://www.ruby-forum.com/t/noob-question-traverse-directory-and-populate-mysql-db/84090>\
**Category:** Ruby\
**Created:** [February 1, 2007, 10:52am UTC](https://www.ruby-forum.com/t/noob-question-traverse-directory-and-populate-mysql-db/84090 "2007-02-01T10:52:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [February 1, 2007, 10:52am UTC](https://www.ruby-forum.com/t/noob-question-traverse-directory-and-populate-mysql-db/84090/1 "2007-02-01T10:52:32Z")

</div>

Hi,

i was hoping for a little bit of direction on a project i’m starting  
that will look to scan a directory structure full of jpgs, and populate  
a db table with various meta data (i.e. filename, path, maybe some exif  
data if i can do that easily).

it’s clear that i’ll need find.find(), ‘mysql’ and a few other nifties.  
it’s more about how to get the data into the db.

would you loop through the find and create a hash for each file, then a  
hash of hashes for all of them, before mapping it into the db?

any comments or pointers gratefully recieved.  
thx

jay

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [February 1, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/noob-question-traverse-directory-and-populate-mysql-db/84090/2 "2007-02-01T20:35:55Z")

</div>

jay schrieb:

> would you loop through the find and create a hash for each file, then a  
> hash of hashes for all of them, before mapping it into the db?
> 
> any comments or pointers gratefully recieved.  
> thx
> 
> jay

Hi jay,

this isn’t optimal but should get you started:

* * *

require ‘mysql’

db = Mysql::new(“localhost”, “user”, “pw”, “testdb”)  
db.query(“CREATE TABLE IF NOT EXISTS jpgs (name text, size int)”)

Dir[’/path/to/images/\*\*/\*.jpg’].each do |file|  
db.query(“INSERT INTO jpgs VALUES (’#{file}’, #{File.size(file)})”)  
end

## db.close

if my memory doesn’t play tricks on me it should be _way_ faster  
to use a single insert statement with lots of data - have fun!

cheers

Simon
