Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Find files

Reply
Thread Tools

Find files

 
 
Clement Ow
Guest
Posts: n/a
 
      05-30-2008
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? =)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      05-30-2008
On Fri, May 30, 2008 at 4:46 AM, Clement Ow
<> 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.

 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      05-30-2008
On Fri, May 30, 2008 at 10:46 AM, Clement Ow
<> 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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-30-2008
2008/5/30 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[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? =)


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", /\.(?ls|txt)$/ do |from, to|
print from, " -> ", to, "\n"
# FileUtils.cp from, to
# FileUtils.mv from, to
end

Kind regards

robert


--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-30-2008
Sorry, hit "send" too early.

2008/5/30 Robert Klemme <>:
> 2008/5/30 Clement Ow <>:


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

> 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", /\.(?ls|txt)$/ do |from, to|
> print from, " -> ", to, "\n"
> # FileUtils.cp from, to
> # FileUtils.mv from, to
> end


Cheers

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Clement Ow
Guest
Posts: n/a
 
      06-02-2008
botp wrote:
> On Fri, May 30, 2008 at 10:46 AM, Clement Ow
> <> 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

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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-02-2008
2008/6/2 Clement Ow <>:
> botp wrote:
>> On Fri, May 30, 2008 at 10:46 AM, Clement Ow
>> <> 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

<snip/>
> end #copy


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

robert


--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
Find.find --- returns directories/files backwards Brad Ruby 9 03-10-2007 02:14 PM
Find.find and files in cwd rtilley Ruby 12 03-22-2006 08:46 PM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM



Advertisments