# Find files

**URL:** <https://www.ruby-forum.com/t/find-files/139024>\
**Category:** Ruby\
**Created:** [May 30, 2008, 4:46am UTC](https://www.ruby-forum.com/t/find-files/139024 "2008-05-30T04:46:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [May 30, 2008, 4:46am UTC](https://www.ruby-forum.com/t/find-files/139024/1 "2008-05-30T04:46:43Z")

</div>

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[i] != nil
  $source.each do |y|
    Find.find(y + "/") do |file|
     src1 << file unless $file_exception[i].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[i].each{|x| /#{x}/ =~ File.basename(file)}  
> > that I cant quite figure(i tried figuring for days!) Help anyone? =)

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [May 30, 2008, 11:26am UTC](https://www.ruby-forum.com/t/find-files/139024/2 "2008-05-30T11:26:40Z")

</div>

On Fri, May 30, 2008 at 4:46 AM, Clement Ow  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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[i].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.

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [May 30, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/find-files/139024/3 "2008-05-30T15:58:09Z")

</div>

2008/5/30 Clement Ow [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> $dest = [“U:/test\_1”, “C:/MOVTEST/04-08”, “C:/DEL/04-08”]  
> $source.each do |y|  
> i = i + 1
> 
> > > src1 \<\< file unless $file\_exception[i].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

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [May 30, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/find-files/139024/4 "2008-05-30T16:01:10Z")

</div>

Sorry, hit “send” too early.

2008/5/30 Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 2008/5/30 Clement Ow [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 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[i].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.

> ```
> 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

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 2, 2008, 9:59am UTC](https://www.ruby-forum.com/t/find-files/139024/5 "2008-06-02T09:59:19Z")

</div>

2008/6/2 Clement Ow [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > ```
> > > Find.find(folder + "/") do |file|
> > > 
> > > ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [May 30, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/find-files/139024/6 "2008-05-30T16:25:18Z")

</div>

On Fri, May 30, 2008 at 10:46 AM, Clement Ow  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> d= $d1

where does this $d1 come from?

> ```
> src1.each do |folder|
> Find.find(folder + "/") do |file|
> :
> :
> 
> ```

you cut a lot of text. pls show full source, to make our life easier…

kind regards -botp

---

<div class="post-metadata">

**Author:** ![owc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/74df32/32.png) [@owc](https://www.ruby-forum.com/u/owc)\
**Post date:** [June 2, 2008, 9:16am UTC](https://www.ruby-forum.com/t/find-files/139024/7 "2008-06-02T09:16:21Z")

</div>

botp wrote:

> On Fri, May 30, 2008 at 10:46 AM, Clement Ow  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > d= $d1
> 
> 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[i].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[i] != nil
   $source.each do |y|
     Find.find(y + "/") do |file|
     src1 << file #unless $file_exception[i].each{|x| /#{x}/ =~ 

```

File.basename(file)}  
$file\_exception[i].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
