Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Ruby Whizz out there? Stuck on a Script I'm trying to create

I'm Stuck

Im trying to create a ruby script that will read a excel file and the search a directory for files then if there is a match copy the files to another directory

Theres a more detailed explanation here - http://www.ruby-forum.com/topic/4411186

along with what code i have so far

17 Answers

Maybe I'm missing something but there's no need to re-invent the wheel when you can just use the cut & mv Linux commands and wrap them in a bash oneliner.

file="images.csv"; src="images"; dest="images/clean"; col"1"; for line in $(cat $file); do echo $line | cut -f$col -d',' | xargs -t -i sh -c "mv $src/{}.* $dest"; done;


Just save your excel sheet as a CSV, specify which column contains the image names.

Something like this:

24005,24005.jpg
20007,20007.jpg
20008,20007a.jpg
20009,20008.jpg
20010,20008a.jpg
20021,20010.jpg
20022,20022.jpg
20024,20022a.jpg

If your excel file looks very different from that maybe you could convert it to a csv and post a link to a sansatized version.

In my Excel file i have product codes of products down column A and that is all. The images are named after this product code but if there is more than one image for a product it is prefixed with a, b, c and so on. in the excel spreadsheet there are over 1000 rows of data. and in the directory there are a lot more than 1000 images.

Heres a link to a google docs spreadsheet with the same data as my excel spreadsheet

https://docs.google.com/spreadsheet/ccc?key=0AqnfEI3zsVMcdGNFTnFLajE1dnNka1BQMGIzVy12amc&usp=sharing

@Jack - Save the Excel sheet as a CSV, then assuming that the server the images are on is a Linux box, you should be all set, just update the variables to the correct paths.

@james - I've downloaded all the images onto my MBP, and everything is set up as is on the link to the ruby-forum. Update what variables?

@Jack

As in in ruby variables in bash are assigned with the equal sign. The file, input, output are variables.


file="/Users/eccleshall/Desktop/ImageCleanUpScript/cleanup.csv"; input="/Users/eccleshall/ImageCleanUp/"; output="/Users/eccleshall/ImageCleanUp/Keep"; for line in $(cat $file); do echo $line | cut -f1 -d',' | xargs -t -i sh -c "mv $input/{}.* $output"; done;


OS X runs a flavor of FreeBSD, as such bash scripts work just fine on OS X, just paste it into a terminal window.

Oh okay, i've never touched bash before.

I tried the script that you pasted above but i get a error.

i have uploaded a image of this error the link is here - https://docs.google.com/file/d/0B6nfEI3zsVMcRkJldTZzY2NIWFk/edit?usp=sharing

as i know nothing about bash i don't know how to debug it

I simplified the script try this:

for line in $(cat "/Users/eccleshall/Desktop/ImageCleanUpScript/cleanup.csv"); do sh -c "mv -v /Users/eccleshall/ImageCleanUp/$line.* /Users/eccleshall/ImageCleanUp/Keep"; done;

Make sure you saved your excel sheet as cleanup.csv


TL;DR - That error is saying the the xargs executable doesn't have a -i switch for the version of xargs on OS X the reason for the incompatibility is that Ubuntu and OS X use different version of xargs. Since your file is just one column, I simplified the script to remove xargs entirely.


If you want to learn about the Unix power tools that come on your Mac you can check out Lynda.com's Unix for Mac OS X course.

Ive just run that but it spat out it all as one directory - i've attached a image to show the output

https://docs.google.com/file/d/0B6nfEI3zsVMcWWlicEdFbk11ODQ/edit?usp=sharing

Il add that course to my to do list cheers

The formatting might have gotten messed up.

try this:

for line in $(cat "/Users/eccleshall/Desktop/ImageCleanUpScript/cleanup.csv"); do sh -c "mv -v /Users/eccleshall/ImageCleanUp/$line.* /Users/eccleshall/ImageCleanUp/Keep"; done;

Hmmm.... strange getting the same error

@Jack -

I ran a test of creating 1000 empty files, and tested the script against it and it worked fine. Might be something in the difference between OS X and Linux. I don't have access to an OS X box so I can't say for sure.

Sorry I couldn't be of more help.

Oh strange, what version of linux are you using? i might create a virtual machine and see if it works that way

Thanks for the help so far!

@Jack - Ubuntu 12.04

@James - Ive now installed installed Ubuntu under a VM and have set up the image directory under /home/eccleshall/ImageCleanUp/ - keep directory under /home/eccleshall/ImageCleanUp/Keep and the csv file is located at /home/eccleshall/ImageCleanUpScript/cleanup.csv

But i am getting a error saying that the file name is too long

see image - https://docs.google.com/file/d/0B6nfEI3zsVMcWkVrNjd3OWQ1NlU/edit?usp=sharing

I created the same directories and moved my test image files to them, to match the setup you have on your VM.

I'm starting to wonder if the quote marks are being munged, make sure to use double quotes and not 2 single quotes.

Have you tried re-typing the script by hand?

for line in $(cat "/home/eccleshall/ImageCleanUpScript/cleanup.csv"); do sh -c "mv -v /home/eccleshall/ImageCleanUp/$line.* /home/eccleshall/ImageCleanUp/Keep/"; done;

@James - just re typed it in and i'm getting the same thing, does it work on you computer?