# Ruby Script read excel file to then read a directo

**URL:** <https://www.ruby-forum.com/t/ruby-script-read-excel-file-to-then-read-a-directo/226817>\
**Category:** Ruby\
**Created:** [February 21, 2013, 10:51pm UTC](https://www.ruby-forum.com/t/ruby-script-read-excel-file-to-then-read-a-directo/226817 "2013-02-21T22:51:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack\_e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@jack\_e](https://www.ruby-forum.com/u/jack_e)\
**Post date:** [February 21, 2013, 10:51pm UTC](https://www.ruby-forum.com/t/ruby-script-read-excel-file-to-then-read-a-directo/226817/1 "2013-02-21T22:51:40Z")

</div>

at the moment i have a excel spreadsheet with a list of the current  
images by product code (17153, 17154 etc) and i have to go through the  
directory manually searching for each image associated with the product  
( 17153.jpg, 17153a.jpg, 17153b.jpg, 17154.jpg etc)

I am wanting to create a script that goes through a excel file cell by  
cell (column A) to then find the corresponding images (17153.jpg,  
17153a.jpg, 17153b.jpg, etc) in a directory and then copy these image in  
another directory

Regards

---

<div class="post-metadata">

**Author:** ![jack\_e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@jack\_e](https://www.ruby-forum.com/u/jack_e)\
**Post date:** [February 21, 2013, 11:20pm UTC](https://www.ruby-forum.com/t/ruby-script-read-excel-file-to-then-read-a-directo/226817/2 "2013-02-21T23:20:42Z")

</div>

Treat this as a step-by step process:

Step 1: Read the data from the spreadsheet. Try a gem like  
“spreadsheet”, “roo”, or any of the other great ones which can handle  
reading excel documents.

Step 2: Find the files. Look up Dir.glob or Ruby’s “Find”.

Step 3: Move the files. Look up Ruby’s File or Fileutils. There are  
several options there.

---

<div class="post-metadata">

**Author:** ![jack\_e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@jack\_e](https://www.ruby-forum.com/u/jack_e)\
**Post date:** [February 22, 2013, 4:47pm UTC](https://www.ruby-forum.com/t/ruby-script-read-excel-file-to-then-read-a-directo/226817/3 "2013-02-22T16:47:09Z")

</div>

I have now come up with this script that does part one but im getting a  
error on part 2

dont know if anyone could help

regards

require ‘fileutils’  
require ‘spreadsheet’

input = “/Users/eccleshall/ImageCleanUp/” #where images are stored  
output = “/Users/eccleshall/ImageCleanUp/Keep” #where images will be  
copied to

book = Spreadsheet.open  
‘/Users/eccleshall/Desktop/ImageCleanUpScript/B002.xls’ #opens workbook

sheet1 = book.worksheet 0 # sets worksheet

sheet1.each do |row| #for each row output  
puts row

```
Dir.glob("/Users/eccleshall/ImageCleanUp/" row).each do|f| #search 

```

/Users/eccleshall/ImageCleanUp/ for files startig with row  
puts f  
end  
end

error i’m getting

ImageCleanUp.rb:14: syntax error, unexpected tIDENTIFIER, expecting ‘)’  
…s/eccleshall/ImageCleanUp/" row).each do|f| #search /Users/e…

---

<div class="post-metadata">

**Author:** ![jack\_e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@jack\_e](https://www.ruby-forum.com/u/jack_e)\
**Post date:** [February 23, 2013, 12:43pm UTC](https://www.ruby-forum.com/t/ruby-script-read-excel-file-to-then-read-a-directo/226817/4 "2013-02-23T12:43:21Z")

</div>

Excel File and directory are set up like this

Excel File Directory  
24005 24005.jpg  
20007 20007.jpg  
20008 20007a.jpg  
20009 20008.jpg  
20010 20008a.jpg  
20021 20010.jpg  
20022 20022.jpg  
20024 20022a.jpg  
20022b.jpg  
20024.jpg

---

<div class="post-metadata">

**Author:** ![jack\_e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/46a35a/32.png) [@jack\_e](https://www.ruby-forum.com/u/jack_e)\
**Post date:** [February 23, 2013, 11:00am UTC](https://www.ruby-forum.com/t/ruby-script-read-excel-file-to-then-read-a-directo/226817/5 "2013-02-23T11:00:19Z")

</div>

You’re missing the wildcard from the search, which would defeat the  
point of using glob, that case you might as well just specify the  
filename directly.

Isn’t “row” an array? I’m not sure you can add the array to a string  
like that. To debug, try breaking this down into smaller steps, each  
with their own variable assignation and debugging output.

Compare the filename strings you’re picking up from the sheet to the  
real filenames you have to check for differences.

I dont’ know how the spreadsheet data compares to the file itself, but  
you could try something like  
("/Users/eccleshall/ImageCleanUp/#{ row[0] }\*")  
That is: your directory path + the first cell of data in your row + a  
wildcard to account for file extensions.
