# Problem using FileUtils to sort JPEG files

**URL:** <https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540>\
**Category:** ruby-talk\
**Created:** [21 October 2007 04:12 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540 "2007-10-21T04:12:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![forgottenwizard](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forgottenwizard](https://rubytalk.org/u/forgottenwizard)\
**Post date:** [21 October 2007 04:12 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/1 "2007-10-21T04:12:44Z")

</div>

This is an odd problem, I admit. I'm currently working on a short script  
to sort a set of JPEGs by size, but right now I'm just trying to move  
them from point A to point B (I'm still learning Ruby, so I do this to  
try and figure things out before getting into the big stuff), and so far  
I have gotten so far as:

####################CODE####################

#!/usr/bin/ruby -wd

require "RMagick"  
require "fileutils"

Dir.chdir(ARGV[0])

for pix in Dir.glob("\*.{jpeg,jpg,png}")  
&nbsp;&nbsp;filetype = Magick::Image::read(pix).first.format  
&nbsp;&nbsp;workdir = Dir.pwd  
&nbsp;&nbsp;If File.directory?(filetype) then  
&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move("#{pix}", "#{workdir}/#{filetype}")  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir(filetype)  
&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move("#{pix}", "#{workdir}/#{filetype}")  
&nbsp;&nbsp;end  
end

####################//CODE####################

The error I keep getting these error messages:

Exception 'Errno::ENOENT' at /usr/lib/ruby/1.8/fileutils.rb:1420 - no  
such file or directory - \<insert pathname to where jpg is being moved

> 

Exception 'Errno::ENOENT' at /usr/lib/ruby/1.8/fileutils.rb:1200 - no  
such file or directory - \<insert pathname to where jpg is being moved

> 

for every file. These errors come, while the file is STILL moved. I'm  
not sure what the error is, but its a noisy one (testdir I made for  
trying this out has ~1900 files in it).

Any clue as to the problem?

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [21 October 2007 06:41 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/2 "2007-10-21T06:41:02Z")

</div>

forgottenwizard wrote:

> The error I keep getting these error messages:
> 
> Exception 'Errno::ENOENT' at /usr/lib/ruby/1.8/fileutils.rb:1420 - no  
> such file or directory - \<insert pathname to where jpg is being moved  
> \>
> 
> Any clue as to the problem?

1) pix is already a string, so doing this:

#{pix}"

is unnecessary.

2) The following causes a syntax error:

If File.directory?(filetype) then

To see a similar syntax error, try this:

num = 10  
If num == 10  
&nbsp;&nbsp;print 'yes'  
end

3) Your default loop probably should be each(), unless you have a  
specific reason to use another loop:

Dir.glob("\*.{jpeg,jpg,png}").each do |fname|

A for-loop has a different scope than each(), i.e. it does not create a  
new scope. An each() loop creates a new scope, although it is leaky(see  
example below).

4) I couldn't find any documentation for the RMagick format() method,  
which is pretty typical of the horrible ruby documentation, however this  
works error free for me:

require 'fileutils'

Dir.chdir("../some\_dir")  
workdir = Dir.pwd #outside of each block

Dir.glob("\*.{jpeg,jpg,png}").each do |fname|  
&nbsp;&nbsp;filetype = fname.split(".")[-1]

&nbsp;&nbsp;if File.directory?(filetype)  
&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move(fname, "#{workdir}/#{filetype}") #workdir is  
accessible inside each()  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir(filetype)  
&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move(fname, "#{workdir}/#{filetype}")  
&nbsp;&nbsp;end  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [21 October 2007 18:05 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/3 "2007-10-21T18:05:01Z")

</div>

> This is an odd problem, I admit. I'm currently working on a short script  
> to sort a set of JPEGs by size, but right now I'm just trying to move  
> them from point A to point B (I'm still learning Ruby, so I do this to  
> try and figure things out before getting into the big stuff), and so far  
> I have gotten so far as:
> 
> ####################CODE####################
> 
> #!/usr/bin/ruby -wd
> 
> require "RMagick"  
> require "fileutils"
> 
> Dir.chdir(ARGV[0])
> 
> for pix in Dir.glob("\*.{jpeg,jpg,png}")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filetype = Magick::Image::read(pix).first.format  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;workdir = Dir.pwd  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If File.directory?(filetype) then

Are you telling us that this is the exact code that you  
ran? It can't be. Ruby is case-sensitive. It's "if",  
not "If". How did "If" get into this code? Didn't you  
copy and paste? Or did you retype it?

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move("#{pix}", "#{workdir}/#{filetype}")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir(filetype)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move("#{pix}", "#{workdir}/#{filetype}")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> end

require "fileutils"

Dir.chdir( ARGV[0] )  
workdir = Dir.pwd

Dir["\*.{jpeg,jpg,png}"].each{|pix|  
&nbsp;&nbsp;filetype = File.extname( pix )[1..-1]  
&nbsp;&nbsp;Dir.mkdir( filetype ) if not File.exists?( filetype )  
&nbsp;&nbsp;FileUtils.move( pix, "#{workdir}/#{filetype}" )  
}

> **···**
>
> On Oct 20, 11:12 pm, forgottenwizard \<phrexianrea...@hushmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [21 October 2007 13:20 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/4 "2007-10-21T13:20:23Z")

</div>

7stud -- wrote:  
&nbsp;&nbsp;\> 4) I couldn't find any documentation for the RMagick format() method,

> which is pretty typical of the horrible ruby documentation, however this works error free for me:

The RMagick doc is not typical. It is the result of many hundreds of hours of my work.

[http://www.simplesystems.org/RMagick/doc/imageattrs.html#format](http://www.simplesystems.org/RMagick/doc/imageattrs.html#format)

> **···**
>
> --  
> RMagick OS X Installer [[http://rubyforge.org/projects/rmagick/\](http://rubyforge.org/projects/rmagick/%5C)]  
> RMagick Hints & Tips [[http://rubyforge.org/forum/forum.php?forum\_id=1618\](http://rubyforge.org/forum/forum.php?forum_id=1618%5C)]  
> RMagick Installation FAQ [[http://rmagick.rubyforge.org/install-faq.html\](http://rmagick.rubyforge.org/install-faq.html%5C)]

---

<div class="post-metadata">

**Author:** ![forgottenwizard](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forgottenwizard](https://rubytalk.org/u/forgottenwizard)\
**Post date:** [21 October 2007 20:52 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/5 "2007-10-21T20:52:41Z")

</div>

> 1) pix is already a string, so doing this:
> 
> #{pix}"
> 
> is unnecessary.

I do that in places to make sure there isn't a problem somewhere with a  
command taking a wrong arg. Unneeded it may be, but it makes me feel  
better when I'm debugging.

> 2) The following causes a syntax error:
> 
> If File.directory?(filetype) then
> 
> To see a similar syntax error, try this:
> 
> num = 10  
> If num == 10  
> &nbsp;&nbsp;print 'yes'  
> end

Do you have a better suggestion? I was thinking of moving that outside  
of the for loop and only doing it once, but looping through everything  
twice (once to check to see if the directories exist and/or make them,  
and a second to sort), but leaving it in makes a little more sense to  
me.

Can you help explain the exact error, or is it just saying that the  
directory is already there?

> 3) Your default loop probably should be each(), unless you have a  
> specific reason to use another loop:
> 
> Dir.glob("\*.{jpeg,jpg,png}").each do |fname|
> 
> A for-loop has a different scope than each(), i.e. it does not create a  
> new scope. An each() loop creates a new scope, although it is leaky(see  
> example below).

That comes from using bash/C before. I'll look into trying it with each  
soon.

> 4) I couldn't find any documentation for the RMagick format() method,  
> which is pretty typical of the horrible ruby documentation, however this  
> works error free for me:
> 
> require 'fileutils'
> 
> Dir.chdir("../some\_dir")  
> workdir = Dir.pwd #outside of each block
> 
> Dir.glob("\*.{jpeg,jpg,png}").each do |fname|  
> &nbsp;&nbsp;filetype = fname.split(".")[-1]
> 
> &nbsp;&nbsp;if File.directory?(filetype)  
> &nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move(fname, "#{workdir}/#{filetype}") #workdir is  
> accessible inside each()  
> &nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir(filetype)  
> &nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move(fname, "#{workdir}/#{filetype}")  
> &nbsp;&nbsp;end  
> end

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. I never fully  
trusted just relying on extensions.

And yes, I do notice the flaw in my logic here, and I'm going to correct  
it within Dir.glob now 🙂

> **···**
>
> On 15:41 Sun 21 Oct , 7stud -- wrote:

---

<div class="post-metadata">

**Author:** ![forgottenwizard](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forgottenwizard](https://rubytalk.org/u/forgottenwizard)\
**Post date:** [21 October 2007 20:56 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/6 "2007-10-21T20:56:23Z")

</div>

Retyped it. uxterm doesn't like copy+paste.

> **···**
>
> On 03:05 Mon 22 Oct , William James wrote:
> 
> > On Oct 20, 11:12 pm, forgottenwizard \<phrexianrea...@hushmail.com\> \> wrote:  
> > \> This is an odd problem, I admit. I'm currently working on a short script  
> > \> to sort a set of JPEGs by size, but right now I'm just trying to move  
> > \> them from point A to point B (I'm still learning Ruby, so I do this to  
> > \> try and figure things out before getting into the big stuff), and so far  
> > \> I have gotten so far as:  
> > \>  
> > \> ####################CODE####################  
> > \>  
> > \> #!/usr/bin/ruby -wd  
> > \>  
> > \> require "RMagick"  
> > \> require "fileutils"  
> > \>  
> > \> Dir.chdir(ARGV[0])  
> > \>  
> > \> for pix in Dir.glob("\*.{jpeg,jpg,png}")  
> > \> filetype = Magick::Image::read(pix).first.format  
> > \> workdir = Dir.pwd  
> > \> If File.directory?(filetype) then
> > 
> > Are you telling us that this is the exact code that you  
> > ran? It can't be. Ruby is case-sensitive. It's "if",  
> > not "If". How did "If" get into this code? Didn't you  
> > copy and paste? Or did you retype it?

---

<div class="post-metadata">

**Author:** ![forgottenwizard](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forgottenwizard](https://rubytalk.org/u/forgottenwizard)\
**Post date:** [21 October 2007 20:58 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/7 "2007-10-21T20:58:30Z")

</div>

Since you wrote the docs for RMagick (or alot of it, I'm guessing), can  
you tell me how to have RMagick handle diffrent filetypes properly? I've  
tried to have it check to see if a file was of an image type, and if not  
ignore it (which would be closer to ideal), but I don't know how to do  
that, and I havn't seen any docs on doing such.

> **···**
>
> On 22:20 Sun 21 Oct , Tim Hunter wrote:
> 
> > 7stud -- wrote:  
> > \> 4) I couldn't find any documentation for the RMagick format() method,
> > 
> > > which is pretty typical of the horrible ruby documentation, however this  
> > > works error free for me:
> > 
> > The RMagick doc is not typical. It is the result of many hundreds of hours  
> > of my work.
> > 
> > [RMagick 1.15.0: class Image (attribute methods)](http://www.simplesystems.org/RMagick/doc/imageattrs.html#format)

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [21 October 2007 21:42 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/8 "2007-10-21T21:42:09Z")

</div>

forgottenwizard wrote:

> **···**
>
> > On 22:20 Sun 21 Oct , Tim Hunter wrote:
> > 
> > > 7stud -- wrote:  
> > > \> 4) I couldn't find any documentation for the RMagick format() method,
> > > 
> > > > which is pretty typical of the horrible ruby documentation, however this works error free for me:
> > > 
> > > The RMagick doc is not typical. It is the result of many hundreds of hours of my work.
> > > 
> > > [RMagick 1.15.0: class Image (attribute methods)](http://www.simplesystems.org/RMagick/doc/imageattrs.html#format)
> > 
> > Since you wrote the docs for RMagick (or alot of it, I'm guessing), can  
> > you tell me how to have RMagick handle diffrent filetypes properly? I've  
> > tried to have it check to see if a file was of an image type, and if not  
> > ignore it (which would be closer to ideal), but I don't know how to do  
> > that, and I havn't seen any docs on doing such.
> 
> I wrote it all.
> 
> To use RMagick to determine if an arbitrary file is an image file that it (that is, ImageMagick) can handle, try to read it. If it's not an image file, it'll raise an exception.
> 
> --  
> RMagick OS X Installer [[http://rubyforge.org/projects/rmagick/\](http://rubyforge.org/projects/rmagick/%5C)]  
> RMagick Hints & Tips [[http://rubyforge.org/forum/forum.php?forum\_id=1618\](http://rubyforge.org/forum/forum.php?forum_id=1618%5C)]  
> RMagick Installation FAQ [[http://rmagick.rubyforge.org/install-faq.html\](http://rmagick.rubyforge.org/install-faq.html%5C)]

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [22 October 2007 02:16 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/9 "2007-10-22T02:16:12Z")

</div>

Tim Hunter wrote:

> I wrote it all.

Then here's a suggestion. On the documentation page, display a link  
that says:

all RMagick methods

When the user clicks on the link, display a page with an alphabetical  
listing of all the methods in RMagick--where each listing is a link to  
the documentation for that method. Easy peasy, Japaneasy.

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|  
&nbsp;&nbsp;if not File.directory?(type)  
&nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir(type)  
&nbsp;&nbsp;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:

lookup = filetypes.inject({}) do |hash, type|  
&nbsp;&nbsp;hash[type] = true  
&nbsp;&nbsp;hash  
end  
#lookup = {'jpeg'=\>true,'jpg'=\>true,'png'=\>true}

glob\_str = filetypes.join(",")

Dir.glob("\*.{#{glob\_str}}").each do |fname|  
&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;filetype = Magick::Image::read(pix).first.format  
&nbsp;&nbsp;&nbsp;&nbsp;#If that causes an error, then execution jumps  
&nbsp;&nbsp;&nbsp;&nbsp;#down to the rescue block below.

&nbsp;&nbsp;&nbsp;&nbsp;if lookup[filetype]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move(fname, "./#{filetype}")  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;rescue IOError  
&nbsp;&nbsp;&nbsp;&nbsp;#do nothing  
&nbsp;&nbsp;end  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![forgottenwizard](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forgottenwizard](https://rubytalk.org/u/forgottenwizard)\
**Post date:** [26 October 2007 18:28 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/10 "2007-10-26T18:28:54Z")

</div>

> 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|  
> &nbsp;&nbsp;if not File.directory?(type)  
> &nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir(type)  
> &nbsp;&nbsp;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|  
> &nbsp;&nbsp;hash[type] = true  
> &nbsp;&nbsp;hash  
> end  
> #lookup = {'jpeg'=\>true,'jpg'=\>true,'png'=\>true}
> 
> glob\_str = filetypes.join(",")
> 
> Dir.glob("\*.{#{glob\_str}}").each do |fname|  
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;filetype = Magick::Image::read(pix).first.format  
> &nbsp;&nbsp;&nbsp;&nbsp;#If that causes an error, then execution jumps  
> &nbsp;&nbsp;&nbsp;&nbsp;#down to the rescue block below.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if lookup[filetype]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.move(fname, "./#{filetype}")  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;rescue IOError  
> &nbsp;&nbsp;&nbsp;&nbsp;#do nothing  
> &nbsp;&nbsp;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?

> **···**
>
> On 11:16 Mon 22 Oct , 7stud -- wrote:

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [26 October 2007 22:33 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/11 "2007-10-26T22:33:25Z")

</div>

> It makes a  
> bit more sense to me to check to make sure the dir exist  
> while looking through everything

Consider this: how does ruby determine if a filename is a directory?  
Suppose your directory has 2,000 files in it. Next, suppose you ask  
ruby if "my\_file.jpg" is a directory. What if ruby first has to look at  
each and every one of those 2,000 files to look for a match with  
filename? Then if it found a match, ruby had to look at the stats for  
the file to determine if it's a directory. That would mean that every  
time you ask if a filename is a directory, ruby has to step through  
every one of those 2,000 files to look for a match. Do you think that  
is very efficient?

> I added in the rescue  
> block the same as you have, and it still exists the loop when run over  
> the script itself.

I have no idea what that means. If you are asking how to determine what  
error to catch with the rescue statement, do this: remove the rescue  
statement from your code and try to read a file that RMagick doesn't  
recognize, and then examine the error message. The error message will  
give the name of the error. For instance, if you run this program:

puts x

the error message is:

"undefined local variable or method `x' for main:Object (NameError)"

To handle that error, you can write:

begin  
&nbsp;&nbsp;puts x  
rescue NameError  
&nbsp;&nbsp;puts "caught the error"  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![forgottenwizard](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forgottenwizard](https://rubytalk.org/u/forgottenwizard)\
**Post date:** [27 October 2007 18:57 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/12 "2007-10-27T18:57:18Z")

</div>

> \> It makes a  
> \> bit more sense to me to check to make sure the dir exist  
> \> while looking through everything
> 
> Consider this: how does ruby determine if a filename is a directory?  
> Suppose your directory has 2,000 files in it. Next, suppose you ask  
> ruby if "my\_file.jpg" is a directory. What if ruby first has to look at  
> each and every one of those 2,000 files to look for a match with  
> filename? Then if it found a match, ruby had to look at the stats for  
> the file to determine if it's a directory. That would mean that every  
> time you ask if a filename is a directory, ruby has to step through  
> every one of those 2,000 files to look for a match. Do you think that  
> is very efficient?

The way I'm currently going through all this, and the cases I'm trying  
to keep from running across (such as a jpg named myfile), what it seems  
to me you are suggesting is going through and check every single file to  
make sure there isn't a new format, and if there is, make a new  
directory, THEN go back though the same directory, find the format  
AGAIN, then sort through everything.

It makes more sense to me to go through and do both at once, since  
(correct me if I am wrong here) it takes longer to find the file format  
as opposed to, say, seeing if there is a directory with the same name as  
that format.

> \>  
> \> I added in the rescue  
> \> block the same as you have, and it still exists the loop when run over  
> \> the script itself.  
> \>
> 
> I have no idea what that means. If you are asking how to determine what  
> error to catch with the rescue statement, do this: remove the rescue  
> statement from your code and try to read a file that RMagick doesn't  
> recognize, and then examine the error message. The error message will  
> give the name of the error. For instance, if you run this program:
> 
> puts x
> 
> the error message is:
> 
> "undefined local variable or method `x' for main:Object (NameError)"
> 
> To handle that error, you can write:
> 
> begin  
> &nbsp;&nbsp;puts x  
> rescue NameError  
> &nbsp;&nbsp;puts "caught the error"  
> end

I'll look more into that. It seems a bad filetype causes several errors,  
so I'm having to go through and take care of each of them.

> **···**
>
> On 07:33 Sat 27 Oct , 7stud -- wrote:

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [29 October 2007 05:54 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/13 "2007-10-29T05:54:31Z")

</div>

Suppose your directory has 2,000 files in it.

The way I see it, in order for your code to determine whether a file's  
type is already a directory, your code has to go searching through a  
directory with 2,000 files in it. And your code does that for each  
filename. Also, if your directory has 10,000 files instead of 2,000  
files, then the search will take five times as long.

The code I posted only has to search through the directory three times  
to set things up, i.e when it creates the directories. After that, the  
code loops over the file names and does a lookup in a hash to determine  
if there's a directory for the file's file type, and hash lookups are  
extremely fast. Which would you rather do: search a directory  
containing 2,000 files 2,000 times; or search a directory containing  
2,000 files 3 times, and then do extremely fast hash lookups 2,000  
times? For the purposes of the comparison, you can assume hash lookups  
take 0 time.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![forgottenwizard](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@forgottenwizard](https://rubytalk.org/u/forgottenwizard)\
**Post date:** [29 October 2007 18:44 UTC](https://rubytalk.org/t/problem-using-fileutils-to-sort-jpeg-files/41540/14 "2007-10-29T18:44:25Z")

</div>

Alright, I see what you are saying. I'll look over your code again,  
since I obviously missed something.

Thanks for the explinasion 🙂

> **···**
>
> On 14:54 Mon 29 Oct , 7stud -- wrote:
> 
> > Suppose your directory has 2,000 files in it.
> > 
> > The way I see it, in order for your code to determine whether a file's  
> > type is already a directory, your code has to go searching through a  
> > directory with 2,000 files in it. And your code does that for each  
> > filename. Also, if your directory has 10,000 files instead of 2,000  
> > files, then the search will take five times as long.
> > 
> > The code I posted only has to search through the directory three times  
> > to set things up, i.e when it creates the directories. After that, the  
> > code loops over the file names and does a lookup in a hash to determine  
> > if there's a directory for the file's file type, and hash lookups are  
> > extremely fast. Which would you rather do: search a directory  
> > containing 2,000 files 2,000 times; or search a directory containing  
> > 2,000 files 3 times, and then do extremely fast hash lookups 2,000  
> > times? For the purposes of the comparison, you can assume hash lookups  
> > take 0 time.
