[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

FTP - Multiple file downloads

Idealone Ideally

4/14/2009 8:56:00 AM

I have written ftp code which will download a single file from the
Server using the below code:

require 'rubygems'
require 'net/ftp'
require 'fileutils'

URL = 'IP address'
username = 'test'
passwd = "test"
filename = "file1"
directory = '/home/test/'
localfile = 'C:\\Documents and Settings\\User1\\Desktop\\file1'
ftp=Net::FTP.new
ftp.connect(URL,21)
ftp.login(username,passwd)
ftp.chdir(directory)
ftp.getbinaryfile(filename,localfile)
ftp.close

But what i am really looking for is:
1) Download multiple files from a given directory.
2) Download files by giving wild characters (eg: *.xls or *.txt)

Please let me know if there is a better way or easier way to achieve
these tasks.

cheers
--
Posted via http://www.ruby-....

5 Answers

Mark Thomas

4/14/2009 11:40:00 AM

0

On Apr 14, 4:55 am, Idealone Ideally <sheka...@gmail.com> wrote:
> I have written ftp code which will download a single file from the
> Server using the below code:
>
> require 'rubygems'
> require 'net/ftp'
> require 'fileutils'
>
> URL = 'IP address'
> username = 'test'
> passwd = "test"
> filename = "file1"
> directory = '/home/test/'
> localfile = 'C:\\Documents and Settings\\User1\\Desktop\\file1'
> ftp=Net::FTP.new
> ftp.connect(URL,21)
> ftp.login(username,passwd)
> ftp.chdir(directory)
> ftp.getbinaryfile(filename,localfile)
> ftp.close
>
> But what i am really looking for is:
> 1) Download multiple files from a given directory.
> 2) Download files by giving wild characters (eg: *.xls or *.txt)
>
> Please let me know if there is a better way or easier way to achieve
> these tasks.

Not all that familiar with the library, but I do recall there is a
list method that accepts wildcards. Try something like this:

files = ftp.list('*.xls')
files.each do |filename|
ftp.getbinaryfile(filename, filename)
end

-- Mark

Idealone Ideally

4/14/2009 1:11:00 PM

0

Mark Thomas wrote:
> On Apr 14, 4:55�am, Idealone Ideally <sheka...@gmail.com> wrote:
>> filename = "file1"
>> 1) Download multiple files from a given directory.
>> 2) Download files by giving wild characters (eg: *.xls or *.txt)
>>
>> Please let me know if there is a better way or easier way to achieve
>> these tasks.
>
> Not all that familiar with the library, but I do recall there is a
> list method that accepts wildcards. Try something like this:
>
> files = ftp.list('*.xls')
> files.each do |filename|
> ftp.getbinaryfile(filename, filename)
> end
>
> -- Mark

I tried using the above code, i am getting some error:

NetBeans 6.5/ruby2/jruby-1.1.4/lib/ruby/1.8/net/ftp.rb:494:in `open': Is
a directory - Is a directory (Errno::EISDIR)
from C:/Program Files/NetBeans
6.5/ruby2/jruby-1.1.4/lib/ruby/1.8/net/ftp.rb:494:in `getbinaryfile'
from C:\user\workspace\qa\Cvis-ruby\lib\ftp_config.rb:25
from C:\user\workspace\qa\Cvis-ruby\lib\ftp_config.rb:24:in
`each'
from C:\user\workspace\qa\Cvis-ruby\lib\ftp_config.rb:24


-- cheers
--
Posted via http://www.ruby-....

Mark Thomas

4/14/2009 2:11:00 PM

0

What to do when you get an error:
1. Read the error message. It might have a clue as to what the problem
is.
2. Scan the backtrace for method names you may have called.
3. Analyze what situation might have caused the method(s) to throw the
error.

Enrique Meza

4/21/2009 7:15:00 PM

0

That's why,

files = ftp.list('*.xls')
p files

Returns an array of file information
--
Posted via http://www.ruby-....

Mark Thomas

4/21/2009 7:25:00 PM

0

No, The OP was trying to open a directory. This information was right
there in the error message. The next step would be for him to find out
why his listing included directories, and omit them.