Find files

Discussion in 'Ruby' started by Clement Ow, May 30, 2008.

  1. Clement Ow

    Clement Ow Guest

    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? =)
    --
    Posted via http://www.ruby-forum.com/.
     
    Clement Ow, May 30, 2008
    #1
    1. Advertising

  2. 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.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.
     
    Jesús Gabriel y Galán, May 30, 2008
    #2
    1. Advertising

  3. Clement Ow

    botp Guest

    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
     
    botp, May 30, 2008
    #3
  4. 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 != 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


    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, May 30, 2008
    #4
  5. 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.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

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, May 30, 2008
    #5
  6. Clement Ow

    Clement Ow Guest

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

    --
    Posted via http://www.ruby-forum.com/.
     
    Clement Ow, Jun 2, 2008
    #6
  7. 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.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
     
    Robert Klemme, Jun 2, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wybo Dekker
    Replies:
    1
    Views:
    365
    Yukihiro Matsumoto
    Nov 15, 2005
  2. rtilley

    Find.find and files in cwd

    rtilley, Mar 21, 2006, in forum: Ruby
    Replies:
    12
    Views:
    214
    ChrisH
    Mar 22, 2006
  3. Brad
    Replies:
    9
    Views:
    160
  4. vdvorkin
    Replies:
    0
    Views:
    413
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    815
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page