If error occurs should not copy again from the beginning

Discussion in 'Ruby' started by Newb Newb, Apr 29, 2009.

  1. Newb Newb

    Newb Newb Guest

    Hi..
    I try to copy folders recursively..

    my_dir = Dir.glob( Dir.glob("#{@source}/**/*")

    my_dir.each do |f|
    File.copy(f,@@dest)
    end

    For example @source = e:/test_dir
    If that test_dir has 10 files.if some error occurs while copying a 4th
    file,Again copying of the file should not continue from 1st file to 10th
    file again.
    If that 4 th file not copied due to any errors it should continue
    copying from 5 th file and so on...

    I think using arrray index might do the trick.
    Any ideas
    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Newb Newb, Apr 29, 2009
    #1
    1. Advertising

  2. Newb Newb

    Heesob Park Guest

    Hi,

    2009/4/29 Newb Newb <>:
    > Hi..
    > I try to copy folders recursively..
    >
    > =C2=A0my_dir =3D Dir.glob( Dir.glob("#{@source}/**/*")
    >
    > =C2=A0my_dir.each do |f|
    > =C2=A0 File.copy(f,@@dest)
    > =C2=A0end
    >
    > For example @source =3D e:/test_dir
    > If that test_dir has 10 files.if some error occurs while copying a 4th
    > file,Again copying of the file should not continue from 1st file to 10th
    > file again.
    > =C2=A0 =C2=A0 If that 4 th file not copied due to any errors it should co=

    ntinue
    > copying from 5 th file and so on...
    >
    > I think using arrray index might do the trick.
    > Any ideas
    > Thanks


    my_dir.each do |f|
    begin
    FileUtils.copy(f,@@dest)
    rescue
    # some error handling
    end
    end

    Or

    my_dir.each do |f|
    FileUtils.copy(f,@@dest) rescue nil
    end


    Regards,

    Park Heesob
    Heesob Park, Apr 29, 2009
    #2
    1. Advertising

  3. Newb Newb

    7stud -- Guest

    Newb Newb wrote:
    > Hi..
    > I try to copy folders recursively..
    >
    > my_dir = Dir.glob( Dir.glob("#{@source}/**/*")
    >
    > my_dir.each do |f|
    > File.copy(f,@@dest)
    > end
    >
    > For example @source = e:/test_dir
    > If that test_dir has 10 files.if some error occurs while copying a 4th
    > file,Again copying of the file should not continue from 1st file to 10th
    > file again.
    > If that 4 th file not copied due to any errors it should continue
    > copying from 5 th file and so on...
    >
    > I think using arrray index might do the trick.
    > Any ideas
    > Thanks


    10.times do |i|
    begin
    puts "reading file #{i}"

    if i == 4
    raise "error reading file #{i}"
    end

    rescue RuntimeError => msg
    puts msg
    next
    end
    end

    --output:--
    reading file 0
    reading file 1
    reading file 2
    reading file 3
    reading file 4
    error reading file 4
    reading file 5
    reading file 6
    reading file 7
    reading file 8
    reading file 9

    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 29, 2009
    #3
  4. Newb Newb

    7stud -- Guest

    7stud -- wrote:


    rescue RuntimeError => msg
    puts msg
    next <---***Don't need that
    end
    end



    --
    Posted via http://www.ruby-forum.com/.
    7stud --, Apr 29, 2009
    #4
    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. Jaime Wyant
    Replies:
    0
    Views:
    330
    Jaime Wyant
    Dec 21, 2004
  2. Tim Peters
    Replies:
    0
    Views:
    613
    Tim Peters
    Dec 21, 2004
  3. Chad
    Replies:
    4
    Views:
    126
    Bob Barrows [MVP]
    Feb 28, 2005
  4. Jesse B.
    Replies:
    9
    Views:
    230
    Jesse B.
    Mar 27, 2010
  5. Geek
    Replies:
    3
    Views:
    144
Loading...

Share This Page