# If error occurs should not copy again from the beginning

**URL:** <https://www.ruby-forum.com/t/if-error-occurs-should-not-copy-again-from-the-beginning/166757>\
**Category:** Ruby\
**Created:** [April 29, 2009, 8:26am UTC](https://www.ruby-forum.com/t/if-error-occurs-should-not-copy-again-from-the-beginning/166757 "2009-04-29T08:26:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [April 29, 2009, 8:26am UTC](https://www.ruby-forum.com/t/if-error-occurs-should-not-copy-again-from-the-beginning/166757/1 "2009-04-29T08:26:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [April 29, 2009, 8:49am UTC](https://www.ruby-forum.com/t/if-error-occurs-should-not-copy-again-from-the-beginning/166757/2 "2009-04-29T08:49:52Z")

</div>

Hi,

2009/4/29 Newb N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [April 29, 2009, 8:59am UTC](https://www.ruby-forum.com/t/if-error-occurs-should-not-copy-again-from-the-beginning/166757/3 "2009-04-29T08:59:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [April 29, 2009, 9:04am UTC](https://www.ruby-forum.com/t/if-error-occurs-should-not-copy-again-from-the-beginning/166757/4 "2009-04-29T09:04:02Z")

</div>

7stud – wrote:

rescue RuntimeError =\> msg  
puts msg  
next \<—\*\*\*Don’t need that  
end  
end
