From: forgottenwizard Date: 2007-10-27T03:28:54+09:00 Subject: Re: Problem using FileUtils to sort JPEG files On 11:16 Mon 22 Oct , 7stud -- wrote: > forgottenwizard wrote: > > > > Except if a file (we'll call it image) is in the dir, it won't get > > sorted, which is why I'm usin RMagick in the first place. > > Ah, I see. I would create the directories outside the loop: > > require 'fileutils' > > Dir.chdir("../somedir") > > filetypes = ["jpeg", "jpg", "png"] > > filetypes.each do |type| > if not File.directory?(type) > Dir.mkdir(type) > end > end > > Then you don't have to check each file's format to see if a > corresponding directory exists. Then based on what Tim Hunter said, you > can do something like this: > I think for now I'm going to keep everything within the loop. It makes a bit more sense to me to check to make sure the dir exist while looking through everything then to go through and hope the first loop got it all. I'll keep that suggestion, though, for a possible rewrite later on. > > lookup = filetypes.inject({}) do |hash, type| > hash[type] = true > hash > end > #lookup = {'jpeg'=>true,'jpg'=>true,'png'=>true} > > glob_str = filetypes.join(",") > > Dir.glob("*.{#{glob_str}}").each do |fname| > begin > filetype = Magick::Image::read(pix).first.format > #If that causes an error, then execution jumps > #down to the rescue block below. > > if lookup[filetype] > FileUtils.move(fname, "./#{filetype}") > end > > rescue IOError > #do nothing > end > end I'm showing how much of a newbie I am here, but I added in the rescue block the same as you have, and it still exists the loop when run over the script itself. What is the best way to figure out what kind of problem there is to throw into the rescue block?