Test equality of each element in an array

1

12 34

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
 
S

Swaroop C H

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
 
R

Rubén Medellín

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
 
Y

yermej

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
 
G

gga

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) {}
 
1

12 34

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.
 
1

12 34

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.
 
T

Todd Benson

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.

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?
 
1

12 34

Ruben said:
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.
 
1

12 34

Todd said:
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.
 
1

12 34

12 said:
Todd said:
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.
 
C

Chris Carter

12 said:
Todd said:
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.

From what you have posted, your ext_not_to_move array does not have
the '.'s before the extension name, which is required.
 
T

Todd Benson

12 said:
Todd said:
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.

Hah! I do this all the time! I try to avoid negatives in my variable
names, but still end up doing it here or there and get totally
confused later.

Todd
 
R

Rubén Medellín

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


You have to do the block test. Any non-empty array will give true if
no block is given. i.e.

[1, 2, 3, 4].any? {|e| e == 4 }
=> true
[1, 2, 3, 4].any? {|e| e == 5 }
=> false
[1, 2, 3, 4].any? #No block given
=> true
[].any? #Empty array, no block given
=> false

Anyway, regexp or include? can solve the problem better. Just pointing
any? as another option.
 
1

12 34

From: (e-mail address removed) :

kind regards -botp

Thanks for the additional comments. I like adding the dot. Funny though,
"unless" is harder for me to grasp; not that I like like negative do not
includes.

PS: How did you get an email address like that? It irritates me that my
address shows in this discussion group. Nothing against this discussion
group (how could I? It's fantastic), it's the software.
 
P

Peña, Botp

T24gQmVoYWxmIE9mIDEyIDM0Og0KIyBGdW5ueSB0aG91Z2gsICJ1bmxlc3MiIGlzIGhhcmRlciBm
b3IgbWUgdG8gZ3Jhc3A7DQoNCnRyeSB1c2luZyBpdCBhcyBhIG1vZGlmaWVyLCBsaWtlDQoNCiAg
IG1vdmVfZmlsZXMgdW5sZXNzIGZpbGVfZXh0LmluP2xpc3Rfbm90X2FsbG93ZWQNCg0KaGVyZSwg
dW5sZXNzIHdvdWxkIGJlIGxpa2UgImJ1dCIgb3IgImV4Y2VwdCIgb3IgImlmIG5vdCIuIEkgZW1w
aGFzaXplIHRoZSBtb3ZpbmcgYnkgc3RhdGluZyBpdCBmaXJzdCwgdGhlbiB0aGUgdW5sZXNzIGFj
dHMgYXMgYSBzcGVjaWFsIGNhc2UuIEkgcmVhbGx5IHNodW4gZnJvbSAiaWYgbm90LyEiIGV4cHJl
c3Npb25zIHNpbmNlIHRoZXkgdGVuZCBwcm9kdWNlIHJ1bm9uIGRvdWJsZS90cmlwbGUgbmVnYXRp
dmVzIHdjIHRlbmQgdG8gdHdpc3QgbXkgYnJhaW4uIGZvcmdpdmUgbWUsIGVuZ2xpc2ggaXMgbm90
IG15IG5hdGl2ZSBsYW5ndWFnZSA6KA0KDQpCdHcsICJpZiIgY2FuIGJlIHVzZWQgYXMgYSBtb2Rp
ZmllciB0b28sIGxpa2UNCg0KICAgbW92ZV9maWxlcyBpZiBub3QgZmlsZV9leHQuaW4/bGlzdF9u
b3RfYWxsb3dlZA0KDQpraW5kIHJlZ2FyZHMgLWJvdHANCg==
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top