Find files

C

Clement Ow

Hi, I have a block of code where I would like to traverese into files
using the Find module and search for files and at the same time have
exceptions to the list of files that I want to eventually select. (These
files are in turn used for copying, moving files)

My block of code is as follows:
$file_exception[0] = [".xls", ".txt"]
$file_exception[1] = [".txt"]

$source= ["U:/dest1(1)", "U:/movtest/source", "U:/movtest/new"]
$dest = ["U:/test_1", "C:/MOVTEST/04-08", "C:/DEL/04-08"]
$selections = [*,*,*]

sd_a=$source.zip($dest,$selections)

sd_a.each do |sd|
$source, $destination, $selections = sd
d= $d1
dst= File.join $destination, d

if $file_exception != nil
$source.each do |y|
Find.find(y + "/") do |file|
src1 << file unless $file_exception.each{|x| /#{x}/ =~
File.basename(file)}
end
end

else
src1 = $source
end

i = i + 1

src1.each do |folder|
Find.find(folder + "/") do |file|
:
:
But apparently, the file exception doesnt seem to work.. It doesnt just
exclude the file exceptions that has the file extentions .txt and .xls,
it excludes the whole list of files in the first 2 source paths, and
only executes the files in the 3rd source path. I presume it should be
something really wrong with the:
src1 << file unless $file_exception.each{|x| /#{x}/ =~ File.basename(file)}

that I cant quite figure(i tried figuring for days!) Help anyone? =)
 
J

Jesús Gabriel y Galán

But apparently, the file exception doesnt seem to work.. It doesnt just
exclude the file exceptions that has the file extentions .txt and .xls,
it excludes the whole list of files in the first 2 source paths, and
only executes the files in the 3rd source path. I presume it should be
something really wrong with the:
src1 << file unless $file_exception.each{|x| /#{x}/ =~ File.basename(file)}

that I cant quite figure(i tried figuring for days!) Help anyone? =)


Sorry, I don't have more time to look deeper into this, but with
regards to the above line,
the each method returns the original enumerable, so in the general
case I think the xxx.each
will never be false, and so the "unless xxx.each" will always happen.

Don't know if this solves your problem though. If I have more time
later I'll try to
take a deeper look.

Jesus.
 
R

Robert Klemme

2008/5/30 Clement Ow said:
Hi, I have a block of code where I would like to traverese into files
using the Find module and search for files and at the same time have
exceptions to the list of files that I want to eventually select. (These
files are in turn used for copying, moving files)

My block of code is as follows:
$file_exception[0] = [".xls", ".txt"]
$file_exception[1] = [".txt"]

$source= ["U:/dest1(1)", "U:/movtest/source", "U:/movtest/new"]
$dest = ["U:/test_1", "C:/MOVTEST/04-08", "C:/DEL/04-08"]
$selections = [*,*,*]

sd_a=$source.zip($dest,$selections)

sd_a.each do |sd|
$source, $destination, $selections = sd
d= $d1
dst= File.join $destination, d

if $file_exception != nil
$source.each do |y|
Find.find(y + "/") do |file|
src1 << file unless $file_exception.each{|x| /#{x}/ =~
File.basename(file)}
end
end

else
src1 = $source
end

i = i + 1

src1.each do |folder|
Find.find(folder + "/") do |file|
:
:
But apparently, the file exception doesnt seem to work.. It doesnt just
exclude the file exceptions that has the file extentions .txt and .xls,
it excludes the whole list of files in the first 2 source paths, and
only executes the files in the 3rd source path. I presume it should be
something really wrong with the:
src1 << file unless $file_exception.each{|x| /#{x}/ =~ File.basename(file)}

that I cant quite figure(i tried figuring for days!) Help anyone? =)


You can do this:

#!/bin/env ruby

require 'find'

def my_find(source, dest, exclude)
Find.find source do |file|
unless exclude === file
dest_file = file.dup
dest_file[0...source.length]=dest
yield file, dest_file
end
end
end

my_find ".", "foo", /\.(?:xls|txt)$/ do |from, to|
print from, " -> ", to, "\n"
# FileUtils.cp from, to
# FileUtils.mv from, to
end

Kind regards

robert
 
R

Robert Klemme

Sorry, hit "send" too early.

2008/5/30 Robert Klemme said:
2008/5/30 Clement Ow <[email protected]>:
But apparently, the file exception doesnt seem to work.. It doesnt just
exclude the file exceptions that has the file extentions .txt and .xls,
it excludes the whole list of files in the first 2 source paths, and
only executes the files in the 3rd source path. I presume it should be
something really wrong with the:
src1 << file unless $file_exception.each{|x| /#{x}/ =~ File.basename(file)}

that I cant quite figure(i tried figuring for days!) Help anyone? =)


You can make your life much simpler by first separating the finding of
files from what you do with them: use a block for the "do" part. Now
you just need a method that finds files, honors exclusions and emits
from and to names. Then you can do anything including moving, copying
and deleting in the block.

Matching is often abstracted by operator === (see case statements for
example). So this is a pretty generic thing for determining matches
or mismatches. Regular expressions do also implement it.
You can do this:

#!/bin/env ruby

require 'find'

def my_find(source, dest, exclude)
Find.find source do |file|
unless exclude === file
dest_file = file.dup
dest_file[0...source.length]=dest
yield file, dest_file
end
end
end

my_find ".", "foo", /\.(?:xls|txt)$/ do |from, to|
print from, " -> ", to, "\n"
# FileUtils.cp from, to
# FileUtils.mv from, to
end

Cheers

robert
 
C

Clement Ow

botp said:
where does this $d1 come from?
It is a date object in the format of MM-YY, in which this object is used
for printing datestamped folders.
src1.each do |folder|
Find.find(folder + "/") do |file|
:
:

you cut a lot of text. pls show full source, to make our life easier..
src1 << file unless $file_exception.each{|x| /#{x}/ =~ File.basename(file)}


Actually how my code works is by finding all the files and then have all
the exclusions that will exclude the files in the block and then finally
executing the move, copy or delete command.

But it is just this selecting and bringing the exclusions part that is
giving me quite abit of problems. But in any case here goes my code:

def copy_r
excep_yyyymmdd = Regexp.compile($exception)
excep_ddmmyyyy = Regexp.compile($exception1)
sd_a=$source.zip($dest,$selections)
src1 = []
i = 0
sd_a.each do |sd|
$source, $destination, $selections = sd
d= $d1
dst= File.join $destination, d

if $file_exception != nil
$source.each do |y|
Find.find(y + "/") do |file|
src1 << file #unless $file_exception.each{|x| /#{x}/ =~
File.basename(file)}
$file_exception.each do |ex|
src1.delete_if {|x| /#{ex}/ =~ File.basename(file)}
end
end
end

else
src1 = $source
end

i = i + 1

src1.each do |folder|
Find.find(folder + "/") do |file|
matchExp = excep_yyyymmdd.match(File.basename(file))
matchExp1 = excep_ddmmyyyy.match(File.basename(file))

if matchExp != nil or matchExp1 != nil
if $keepLastMthDay == false and $keepLastMth == false and
$createDestDir == true
begin
Dir.chdir(dest)
Dir.mkdir(d)
FileUtils.cp_r file, dst
rescue Errno::EEXIST
FileUtils.cp_r file, dst
end #rescue
puts "File Copied: #{file}\nDest: #{dst}"
elsif $keepLastMthDay == false and $keepLastMth == false and
$createDestDir == false
FileUtils.cp_r file, $destination
puts "File Copied: #{file}\nDest: #{$destination}"
elsif $keepLastMthDay == true and $keepLastMth == true
puts "Please select only one GENERAL OPTION: keepLastMth or
keepLastDayMth"
elsif ($keepLastMthDay == true or $keepLastMth == true)
puts "File Escaped: #{file} (Keep last day of month option
activated)"
end # if

elsif matchExp == nil or matchExp1 == nil
if $createDestDir == true
begin
Dir.chdir(dest)
Dir.mkdir(d)
FileUtils.cp_r file, dst #:force => true
rescue Errno::EEXIST
FileUtils.cp_r file, dst #:force => true
end #rescue
puts "File Moved: #{file}\nDest: #{dst}"
elsif $createDestDir == false
FileUtils.cp_r file, $destination #:force => true
puts "File Moved: #{file}\nDest: #{$destination}"
end # if


end # if match

if File.exist?(dst+"/"+File.basename(f)) == true or
File.exist?($destination+"/"+File.basename(f)) == true
puts "File Copy: SUCCESS"
else
#puts "File Copy: FAILED"
end #if
end #do
end #do
end #copy
 
R

Robert Klemme

2008/6/2 Clement Ow said:
botp said:
where does this $d1 come from?
It is a date object in the format of MM-YY, in which this object is used
for printing datestamped folders.
src1.each do |folder|
Find.find(folder + "/") do |file|
:
:

you cut a lot of text. pls show full source, to make our life easier..
src1 << file unless $file_exception.each{|x| /#{x}/ =~ File.basename(file)}


Actually how my code works is by finding all the files and then have all
the exclusions that will exclude the files in the block and then finally
executing the move, copy or delete command.

But it is just this selecting and bringing the exclusions part that is
giving me quite abit of problems. But in any case here goes my code:

def copy_r

end #copy

Chances are that you can condense that code *a lot*. Did you see my reply?

robert
 

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,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top