# File copy

**URL:** <https://www.ruby-forum.com/t/file-copy/164808>\
**Category:** Ruby\
**Created:** [April 6, 2009, 8:54am UTC](https://www.ruby-forum.com/t/file-copy/164808 "2009-04-06T08:54:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 8:54am UTC](https://www.ruby-forum.com/t/file-copy/164808/1 "2009-04-06T08:54:06Z")

</div>

Hi,  
I am facing a problem to copy the file.

I am getting path of the file like this  
“[http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931\_8289306.jpg](http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931_8289306.jpg)”.  
I want to copy this file locally. I am trying to do this by using

File.copy(“[http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931\_8289306.jpg","/images](http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931_8289306.jpg%22,%22/images)”);

I am getting an error  
Invalid argument -  
[http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931\_8289306.jpg](http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931_8289306.jpg)

I know that this is not a directory path. Is any one knows how to copy  
this file?

Thanks  
Tushar

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 9:18am UTC](https://www.ruby-forum.com/t/file-copy/164808/2 "2009-04-06T09:18:56Z")

</div>

> I know that this is not a directory path. Is any one knows how to copy  
> this file?

Hi Gandhi,

Try this code which is mentioned in my blog

> **[Save images from any URL](https://raveendran.wordpress.com/2009/04/06/save-images-from-any-url/)**
>
> Save images from any URL Note: it will works only OS –\> WINDOWS and Browser –\>  IE 1. install Ruby 2. Install watir gem – gem install watir 3. Look this code Code: require &…

Regards,  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 9:59am UTC](https://www.ruby-forum.com/t/file-copy/164808/3 "2009-04-06T09:59:51Z")

</div>

Tushar G. wrote:

> Hi,  
> I am facing a problem to copy the file.
> 
> I am getting path of the file like this  
> “[http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931\_8289306.jpg](http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931_8289306.jpg)”.  
> I want to copy this file locally. I am trying to do this by using
> 
> File.copy(“[http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931\_8289306.jpg","/images](http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931_8289306.jpg%22,%22/images)”);
> 
> I am getting an error  
> Invalid argument -  
> [http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931\_8289306.jpg](http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931_8289306.jpg)
> 
> I know that this is not a directory path. Is any one knows how to copy  
> this file?
> 
> Thanks  
> Tushar

Why can’t you use wget command in system or popen to download it ?  
It’s my suggestion

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 9:58am UTC](https://www.ruby-forum.com/t/file-copy/164808/4 "2009-04-06T09:58:18Z")

</div>

Hi,

I write the code like this  
"  
browser=Watir::IE.new  
browser.visible=false  
browser.goto(params[:photo\_url])

```
idx = 0

# using the images collection, iterate through all of the images on 

```

a page  
browser.images.each do |x|  
idx += 1  
# apparently the string accepted by the string method will not  
allow variable substitution  
original\_type= x.src[-4,4]  
type=“.gif” if original\_type == “.gif”  
type=“.jpg” if original\_type == “.jpg”  
type=“.png"if original\_type == “.png”  
type=”.bmp" if original\_type == “.bmp”  
type=“jpeg” if original\_type == “.jpeg”  
x.save(‘/images/’)  
end  
"  
I am getting an error

" The AutoIt dll must be correctly registered for this feature to work  
properly "  
How to resolve this?  
Thanks,  
Tushar

jazzez ravi wrote:

> > I know that this is not a directory path. Is any one knows how to copy  
> > this file?
> 
> Hi Gandhi,
> 
> Try this code which is mentioned in my blog
> 
> [Save images from any URL – Raveendran](http://raveendran.wordpress.com/2009/04/06/save-images-from-any-url/)
> 
> Regards,  
> P.Raveendran  
> [http://raveendran.wordpress.com](http://raveendran.wordpress.com)

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 10:10am UTC](https://www.ruby-forum.com/t/file-copy/164808/5 "2009-04-06T10:10:53Z")

</div>

Hi,  
I went through this link  
[http://www.mail-archive.com/removed\_email\_address@domain.invalid/msg06628.html](http://www.mail-archive.com/removed_email_address@domain.invalid/msg06628.html)  
and my dll registered successfully.  
I changed my code like this  
"  
browser=Watir::IE.new  
browser.visible=true  
browser.goto(params[:photo\_url])

```
idx = 0

# using the images collection, iterate through all of the images on 

```

a page  
browser.images.each do |x|  
idx += 1  
# apparently the string accepted by the string method will not  
allow variable substitution  
original\_type= x.src[-4,4]  
type=“.gif” if original\_type == “.gif”  
type=“.jpg” if original\_type == “.jpg”  
type=“.png"if original\_type == “.png”  
type=”.bmp" if original\_type == “.bmp”  
type=“jpeg” if original\_type == “.jpeg”  
x.save(‘E:\Rails2.2\integrateSNS\public\images’)  
end  
"  
It open a new tab and open that image and also open windows standard sve  
pop up and hangs.  
What should I do for this?

Thanks,  
Tushar

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 10:08am UTC](https://www.ruby-forum.com/t/file-copy/164808/6 "2009-04-06T10:08:34Z")

</div>

Tushar G. wrote:

> ```
> browser.goto(params[:photo_url])
> 
> ```

Hi Gandhi,

Oops. please try with separate .rb file first.

After that try to implement in rails.

Regards,  
P.Raveendran

> **[Raveendran](https://raveendran.wordpress.com)**
>
> Nothing is impossible...

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 10:19am UTC](https://www.ruby-forum.com/t/file-copy/164808/7 "2009-04-06T10:19:03Z")

</div>

Hi Gandhi,

Please try to run the code in separate .rb file. Update here in case you  
get any errors.

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/file-copy/164808/8 "2009-04-06T17:47:29Z")

</div>

On Mon, Apr 6, 2009 at 12:23 PM, Tushar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi,
> 
> File.copy(“[http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931\_8289306.jpg","/images](http://photos-d.ak.fbcdn.net/photos-ak-snc1/v2574/90/75/704291251/457931_8289306.jpg%22,%22/images)”);

Hello Tushar,

Take a look at ‘net/http’, which is included in the ruby standard  
library:  
[http://ruby-doc.org/core-1.9/classes/Net/HTTP.html](http://ruby-doc.org/core-1.9/classes/Net/HTTP.html)

For example the following code will download and save the ruby logo  
off [ruby-lang.org](http://ruby-lang.org):

require ‘net/http’  
uri = URI.parse(‘[http://www.ruby-lang.org/images/logo.gif](http://www.ruby-lang.org/images/logo.gif)’)  
Net::HTTP.get\_response(uri) do |response|  
File.open(‘/tmp/logo.gif’, ‘w’) {|f| f.write response.body }  
end

There are various other ways to go about doing this - some of which  
have been mentioned above - but ‘net/http’ is a good place to start.

solidarity,  
lasitha.

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [April 6, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/file-copy/164808/9 "2009-04-06T18:33:50Z")

</div>

On Mon, Apr 6, 2009 at 5:45 PM, lasitha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> For example the following code will download and save the ruby logo  
> off [ruby-lang.org](http://ruby-lang.org):

Another possibility:

irb(main):001:0\> require ‘open-uri’  
=\> true  
irb(main):003:0\> open(“[http://www.ruby-lang.org/images/logo.gif](http://www.ruby-lang.org/images/logo.gif)”) do |f|  
irb(main):004:1\* File.open(“logo.gif”, “w”){|out| out.write f.read}  
irb(main):005:1\> end

Regards,

Jesus.
