[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Test equality of each element in an array

12 34

7/1/2007 4:01:00 AM

I want to see if any element of an array is equal to a file extension

Something like

require 'find'
require 'FileUtils'
src="/folder/on/drive/which/contains/other/folder/and/files"
dest ='another place'
extNot2move = %{jpg,JPG,pdf,PDF,MRW}
Find.find(src) do |fn|
if File.file?(fn)
if extNot2Move.{any of the elements} == File.extname(fn) ## this is
the test I'm interested in
FileUtils.Move(src, dest)
end
end

I think I could do it with a extNot2Move.each with a block following,
but it seems like their should something more concise.

Thanks from a Ruby newbie

--
Posted via http://www.ruby-....

15 Answers

Swaroop C H

7/1/2007 4:08:00 AM

0

> if extNot2Move.{any of the elements} == File.extname(fn) ## this is
> the test I'm interested in
> I think I could do it with a extNot2Move.each with a block
> following, but it seems like their should something more concise.

Why not use a regex?

irb(main):002:0> File.extname('foo.jpg') =~ /\.(jpg|JPG|pdf|PDF|MRW)/
=> 0 # 0 is the position where the regex matched
irb(main):003:0> File.extname('foo.zip') =~ /\.(jpg|JPG|pdf|PDF|MRW)/
=> nil


Thanks,
Swaroop

Rubén Medellín

7/1/2007 5:14:00 AM

0

On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:

> if extNot2Move.{any of the elements} == File.extname(fn) ## this is

Surprise.

if extNot2Move.any?{|something| do something}

and, if you only want to match the extension, and trust in
File.extname, you may go with the Regexp

yermej

7/1/2007 5:20:00 AM

0

On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:
> I want to see if any element of an array is equal to a file extension
>

I would use Array's include? method:

ext_not_to_move = ['jpg', 'pdf', 'mrw']
if ext_not_to_move.include? File.extname(fn).downcase
# do stuff
end

And use downcase so you don't have to include variations of each
extension (jpg, JPG, jpG, etc.).

Jeremy

gga

7/1/2007 12:58:00 PM

0

On Jul 1, 1:00 am, 12 34 <rubyfo...@web.knobby.ws> wrote:
> I want to see if any element of an array is equal to a file extension
>
> Something like
>
> require 'find'
> require 'FileUtils'
> src="/folder/on/drive/which/contains/other/folder/and/files"
> dest ='another place'
> extNot2move = %{jpg,JPG,pdf,PDF,MRW}
> Find.find(src) do |fn|
> if File.file?(fn)
> if extNot2Move.{any of the elements} == File.extname(fn) ## this is
> the test I'm interested in
> FileUtils.Move(src, dest)
> end
> end
>

In general, any sort of file operation on a number of files is better
done in a single atomic operation. The reason is that this then makes
it easy to 'undo' the operation in case of failure or in case the user
stops it midway (like with ctrl-c).

A couple of other issues in your code:
- require 'FileUtils' should be 'fileutils'. Your code will be
unportable otherwise.
- FileUtils.Move() does not exist. Use FileUtils.mv or
FileUtils.move.


require 'find'
require 'fileutils'

@files = []
@src = '/folder/on/drive/which/contains/other/folder/and/files'
@dst = 'otherplace'
EXTS = %w( jpg pdf mrw ) # %w() creates array avoiding having to
use , or quotes

#
# Collect the files
#
Find.find(@src) do |elem|
next unless File.file?(elem)
if EXTS.include?( File.extname(elem).downcase )
@files << elem # don't do anything yet, just collect the files
end
end

#
# Simple routine to undo the move
#
def undo_move
@files.each { |f|
src = "#@dst/#{File.basename(f)}"
FileUtils.mv(src, f) if File.exists?(src) and not File.exists?(f)
}
end

#
# Move them
#

trap(INT) { undo_move }

begin
@files.each { |f| FileUtils.mv(f, @dst) }
rescue
undo_move
end

trap(INT) {}


12 34

7/1/2007 4:06:00 PM

0

yermej@gmail.com wrote:
> On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:
>> I want to see if any element of an array is equal to a file extension
>>
>
> I would use Array's include? method:
>
> ext_not_to_move = ['jpg', 'pdf', 'mrw']
> if ext_not_to_move.include? File.extname(fn).downcase
> # do stuff
> end
>
> And use downcase so you don't have to include variations of each
> extension (jpg, JPG, jpG, etc.).
>
> Jeremy

Thanks everyone for the suggestions. Jeremy's appeals to me because it's
the most direct. I looked at include, but didn't realize exactly how it
works. Thanks for the downcase reminder.

I had tried .any, but may not have had it all there.

Great community.

--
Posted via http://www.ruby-....

12 34

7/1/2007 6:06:00 PM

0

yermej@gmail.com wrote:
> On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:
>> I want to see if any element of an array is equal to a file extension
>>
>
> I would use Array's include? method:
>
> ext_not_to_move = ['jpg', 'pdf', 'mrw']
> if ext_not_to_move.include? File.extname(fn).downcase
> # do stuff
> end
>
> Jeremy

As I said, this appealed to me, but it's not working and now that I look
at it more it didn't make sense to this Ruby newbie.

if ext_not_to_move.include? File.extname(fn).downcase

It probably is OK, but I wasn't sure of the syntax (although I don't get
an error). So I changed it to

if extNot2move.include?(File.extname(fn).downcase)

But this is always false. This construct has the advantage I could
puts #{extNot2move.include?(File.extname(fn).downcase)}

I've puts the File.extname(fn).downcase seperately, and it looks good to
me. I have some that should be true.

Partial output (::: are not Ruby, just separtors in the puts):

extNot2move.class is an Array and the elements are
jpg,.pdf,.mrw,.png,.psd,.tif,.gif,.pict

extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
is: /Volumes/2001/Tioga OCSS.x.tif ::: File.extname(fn).downcase is .tif

extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
is: /Volumes/2001/BirdScript.pict ::: File.extname(fn).downcase is .pict

Thanks for any more help.

--
Posted via http://www.ruby-....

Todd Benson

7/1/2007 6:36:00 PM

0

On 7/1/07, 12 34 <rubyforum@web.knobby.ws> wrote:
> yermej@gmail.com wrote:
> > On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:
> >> I want to see if any element of an array is equal to a file extension
> >>
> >
> > I would use Array's include? method:
> >
> > ext_not_to_move = ['jpg', 'pdf', 'mrw']
> > if ext_not_to_move.include? File.extname(fn).downcase
> > # do stuff
> > end
> >
> > Jeremy
>
> As I said, this appealed to me, but it's not working and now that I look
> at it more it didn't make sense to this Ruby newbie.
>
> if ext_not_to_move.include? File.extname(fn).downcase
>
> It probably is OK, but I wasn't sure of the syntax (although I don't get
> an error). So I changed it to
>
> if extNot2move.include?(File.extname(fn).downcase)
>
> But this is always false. This construct has the advantage I could
> puts #{extNot2move.include?(File.extname(fn).downcase)}
>
> I've puts the File.extname(fn).downcase seperately, and it looks good to
> me. I have some that should be true.
>
> Partial output (::: are not Ruby, just separtors in the puts):
>
> extNot2move.class is an Array and the elements are
> .jpg,.pdf,.mrw,.png,.psd,.tif,.gif,.pict
>
> extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
> is: /Volumes/2001/Tioga OCSS.x.tif ::: File.extname(fn).downcase is .tif
>
> extNot2move.include?(File.extname(fn).downcase) is false ::: Filename
> is: /Volumes/2001/BirdScript.pict ::: File.extname(fn).downcase is .pict
>
> Thanks for any more help.
>
> --
> Posted via http://www.ruby-....

This works for me just fine (the exact same thing you are doing):

['.tif''].include? File.extname('/Volumes/2001/Tioga OCSS.x.tif').downcase

=> true


Are you sure you don't accidentally have any extra/weird characters in
either your extNot2Move array or your File names?

12 34

7/1/2007 7:03:00 PM

0

Ruben Medellin wrote:
> On Jun 30, 11:00 pm, 12 34 <rubyfo...@web.knobby.ws> wrote:
>
>> if extNot2Move.{any of the elements} == File.extname(fn) ## this is
>
> Surprise.
>
> if extNot2Move.any?{|something| do something}
>
> and, if you only want to match the extension, and trust in
> File.extname, you may go with the Regexp

Isn't extNot2Move.any? always true? I don't see where the test is made.

--
Posted via http://www.ruby-....

12 34

7/1/2007 10:41:00 PM

0

Todd Benson wrote:
> On 7/1/07, 12 34 <r> wrote:
>
> This works for me just fine (the exact same thing you are doing):
>
> ['.tif''].include? File.extname('/Volumes/2001/Tioga
> OCSS.x.tif').downcase
>
> => true
>
>
> Are you sure you don't accidentally have any extra/weird characters in
> either your extNot2Move array or your File names?

Thanks. Yes this works, and it works when I change one of the tif to txt
for example. So the problem must be in the extNot2move.any? part.

More fiddling and learning.

--
Posted via http://www.ruby-....

12 34

7/1/2007 11:07:00 PM

0

12 34 wrote:
> Todd Benson wrote:
>> On 7/1/07, 12 34 <r> wrote:
>>
>> This works for me just fine (the exact same thing you are doing):
>>
>> ['.tif''].include? File.extname('/Volumes/2001/Tioga
>> OCSS.x.tif').downcase
>>
>> => true
>>
>>
>> Are you sure you don't accidentally have any extra/weird characters in
>> either your extNot2Move array or your File names?
>
> Thanks. Yes this works, and it works when I change one of the tif to txt
> for example. So the problem must be in the extNot2move.any? part.
>
> More fiddling and learning.

if extNot2move.include?(File.extname(fn).downcase) # Does not work

if !['.jpg', '.pdf',
'.mrw','.png','.psd','.tif','.gif','.pict'].include?(File.extname(fn).downcase)
# seems to work

I need to check the logic of what I'm doing after the test. I've got
myself lost in trying out so many variations.

--
Posted via http://www.ruby-....