Forum: Ruby If error occurs should not copy again from the beginning

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3083ca8f0496edaac2fa10c20630d0bc?d=identicon&s=25 Newb Newb (newb)
on 2009-04-29 08:26
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
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-04-29 08:49
(Received via mailing list)
Hi,

2009/4/29 Newb Newb <revathy.p@angleritech.com>:
> 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

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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-29 08:59
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-04-29 09:04
7stud -- wrote:


  rescue RuntimeError => msg
    puts msg
    next       <---***Don't need that
  end
end
This topic is locked and can not be replied to.