# Problems ftp'ing with RUBY

**URL:** <https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382>\
**Category:** Ruby\
**Created:** [May 3, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382 "2006-05-03T17:19:36Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 5:19pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/1 "2006-05-03T17:19:36Z")

</div>

I need to ftp PDF files to our printer. Using the script below, I’m  
trying to do that, but, it ain’t working. It just sits there. I ftp  
manually to the same site to see if anything’s getting there, and, no,  
nothing is. Can someone see what’s wrong here? I’ve tried it with IRB,  
too, but that doesn’t work either.

Thanks.

require ‘net/ftp’  
indexfiles = Dir.glob(“\*.pdf”)  
indexfiles.each do |indexfile|  
ftp = Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|  
ftp.login(‘username’,‘password’)  
ftp.chdir(‘/data/bnaindex’)  
ftp.putbinaryfile(“indexfile”)  
end  
end

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/2 "2006-05-03T19:07:43Z")

</div>

On Thu, 4 May 2006, Peter B. wrote:

try

> require ‘net/ftp’  
> indexfiles = Dir.glob(“\*.pdf”)  
> indexfiles.each do |indexfile|  
> ftp = Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|

ftp.passive = true

> ftp.login(‘username’,‘password’)  
> ftp.chdir(‘/data/bnaindex’)  
> ftp.putbinaryfile(“indexfile”)  
> end  
> end

-a

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/3 "2006-05-03T21:03:01Z")

</div>

unknown wrote:

> On Thu, 4 May 2006, Peter B. wrote:
> 
> try
> 
> > require ‘net/ftp’  
> > indexfiles = Dir.glob(“\*.pdf”)  
> > indexfiles.each do |indexfile|  
> > ftp = Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|
> 
> ftp.passive = true
> 
> > ftp.login(‘username’,‘password’)  
> > ftp.chdir(‘/data/bnaindex’)  
> > ftp.putbinaryfile(“indexfile”)  
> > end  
> > end
> 
> -a

Thanks for your response. Unfortunately, it still didn’t work. For the  
time being, I’ve just re-done my script to exit out and execute a script  
from another language.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 9:08pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/4 "2006-05-03T21:08:35Z")

</div>

Did you run a network snoop to see if the FTP session is even getting  
out of  
your machine?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/5 "2006-05-03T21:14:42Z")

</div>

On 5/3/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘net/ftp’  
> indexfiles = Dir.glob(“\*.pdf”)  
> indexfiles.each do |indexfile|  
> ftp = Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|  
> ftp.login(‘username’,‘password’)  
> ftp.chdir(‘/data/bnaindex’)  
> ftp.putbinaryfile(“indexfile”)

I suppose no one has pointed out that the above line is probably a  
bug. Here you are telling the FTP library to put a binary file called  
indexfile, not using the contents of the variable indexfile. Therein  
lies the problem, as far as I can see.

Regards,  
Ryan

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/6 "2006-05-03T22:20:14Z")

</div>

Francis C. wrote:

> Did you run a network snoop to see if the FTP session is even getting  
> out of  
> your machine?

I don’t know of such a thing. But, I did do a regular command line ftp  
(winxp) and it worked fine. Thanks.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/7 "2006-05-03T21:17:43Z")

</div>

On May 3, 2006, at 5:19 PM, Peter B. wrote:

> require ‘net/ftp’  
> indexfiles = Dir.glob(“\*.pdf”)  
> indexfiles.each do |indexfile|  
> ftp = Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|  
> ftp.login(‘username’,‘password’)  
> ftp.chdir(‘/data/bnaindex’)  
> ftp.putbinaryfile(“indexfile”)  
> ^^^^^^^^^  
> you want indexfile, not “indexfile”  
> end  
> end

Also, you are creating a new connection for every file. You want  
something like:

require ‘net/ftp’  
Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|  
ftp.login(‘username’,‘password’)  
ftp.chdir(‘/data/bnaindex’)  
Dir.glob(“\*.pdf”).each { |file| ftp.putbinaryfile(file) }  
end

– Daniel

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/8 "2006-05-03T22:23:38Z")

</div>

Ryan L. wrote:

> On 5/3/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > require ‘net/ftp’  
> > indexfiles = Dir.glob(“\*.pdf”)  
> > indexfiles.each do |indexfile|  
> > ftp = Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|  
> > ftp.login(‘username’,‘password’)  
> > ftp.chdir(‘/data/bnaindex’)  
> > ftp.putbinaryfile(“indexfile”)
> 
> I suppose no one has pointed out that the above line is probably a  
> bug. Here you are telling the FTP library to put a binary file called  
> indexfile, not using the contents of the variable indexfile. Therein  
> lies the problem, as far as I can see.
> 
> Regards,  
> Ryan

You mean, I shouldn’t have it in quotes, right? Or, should I use  
(#{indexfile})? I have been told in the past to use that #{  
nomenclature. Now I see why. Thank you!

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/9 "2006-05-03T22:25:17Z")

</div>

Daniel H. wrote:

> On May 3, 2006, at 5:19 PM, Peter B. wrote:
> 
> > require ‘net/ftp’  
> > indexfiles = Dir.glob(“\*.pdf”)  
> > indexfiles.each do |indexfile|  
> > ftp = Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|  
> > ftp.login(‘username’,‘password’)  
> > ftp.chdir(‘/data/bnaindex’)  
> > ftp.putbinaryfile(“indexfile”)  
> > ^^^^^^^^^  
> > you want indexfile, not “indexfile”  
> > end  
> > end
> 
> Also, you are creating a new connection for every file. You want  
> something like:
> 
> require ‘net/ftp’  
> Net::FTP.open(‘[mpc.bna.com](http://mpc.bna.com)’) do |ftp|  
> ftp.login(‘username’,‘password’)  
> ftp.chdir(‘/data/bnaindex’)  
> Dir.glob(“\*.pdf”).each { |file| ftp.putbinaryfile(file) }  
> end
> 
> – Daniel

Thanks. I’ll try this. The reason I was doing it for every file was  
because, in my real, live file, I’m sending a notification e-mail for  
each file sent, with the filename in the subject line. I suppose I could  
still do that, after sending all the files en-masse. Just to a mail  
“each” for each PDF.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 3, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/10 "2006-05-03T22:29:07Z")

</div>

Apart from the fact the Leavengood (upthread) has probably spotted your  
problem, try ethereal on Windows if you’re still having trouble. It’ll  
at  
least tell you if your problem is local.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 4, 2006, 2:13am UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/11 "2006-05-04T02:13:13Z")

</div>

On 5/4/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You mean, I shouldn’t have it in quotes, right? Or, should I use  
> (#{indexfile})? I have been told in the past to use that #{  
> nomenclature. Now I see why. Thank you!

The #{var} syntax is for embedding a variable in a string.

str = “Hello #{username}, welcome to the Bat Cave.”

if you’re not in a string, don’t use #{var}, the unadorned variable name  
will give you the variable.

say\_hello\_to(username)

Cheers

;Daniel

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 4, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/12 "2006-05-04T15:52:22Z")

</div>

Francis C. wrote:

> Apart from the fact the Leavengood (upthread) has probably spotted your  
> problem, try ethereal on Windows if you’re still having trouble. It’ll  
> at  
> least tell you if your problem is local.

I’ve googled it and I’ll download it. Thanks. But, I do know that many,  
many files are going from my company to our printer, for printing, so, I  
don’t think it’s a network issue. I just think that my RUBY is screwy.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [May 4, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/problems-ftping-with-ruby/94382/13 "2006-05-04T15:53:33Z")

</div>

Daniel B. wrote:

> On 5/4/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > You mean, I shouldn’t have it in quotes, right? Or, should I use  
> > (#{indexfile})? I have been told in the past to use that #{  
> > nomenclature. Now I see why. Thank you!
> 
> The #{var} syntax is for embedding a variable in a string.
> 
> str = “Hello #{username}, welcome to the Bat Cave.”
> 
> if you’re not in a string, don’t use #{var}, the unadorned variable name  
> will give you the variable.
> 
> say\_hello\_to(username)
> 
> Cheers
> 
> ;Daniel

Still now working. But, I appreciate very much your explanation of #{}.  
It helps a lot.
