473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FTP - Multiple file downloads using ruby

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

Expand|Select|Wrap|Line Numbers
  1. require 'rubygems'
  2. require 'net/ftp'
  3. require 'fileutils'
  4.  
  5. URL = 'IP address'
  6. username = 'test'
  7. passwd = "test"
  8. filename = "file1"
  9. directory = '/home/test/'
  10. localfile = 'C:\\Documents and Settings\\User1\\Desktop\\file1'
  11. ftp=Net::FTP.new
  12. ftp.connect(URL,21)
  13. ftp.login(username,passwd)
  14. ftp.chdir(directory)
  15. ftp.getbinaryfile(filename,localfile)
  16. 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
Apr 15 '09 #1
5 12047
improvcornartist
303 Expert 100+
If you know the filenames, you should be able to loop through them. If you need to list the files, you can try the ftp methods list or nlst. These allow wildcards. You would then loop through the list of filenames returned. See the documentation for additional info.
Apr 15 '09 #2
Thanks for the reply, I am novice to ruby & programming, it will be great if you could give me the sample code for the looping.
Because i dont want to hardcode the file names.
For example: If i have 5 excel files in my server with different names, i would like to download them with the *.xls, but as i see with the getbinary.file method is that i can download only one file at a time!!!
Correct me if i am wrong!!
Apr 15 '09 #3
improvcornartist
303 Expert 100+
Something like this would loop the filenames and get the files. Note that if you use the localfile variable in getbinaryfile, each file will overwrite the previous file unless you change the variable. You could do that by looping by index instead of value.
Expand|Select|Wrap|Line Numbers
  1. ftp.chdir(directory)
  2. filenames = ftp.nlst('*.xls') #This is an array of all Excel files in the ftp directory
  3. #Loop by value
  4. filenames.each{|filename| #Loop through each element of the array
  5.   ftp.getbinaryfile(filename,filename) #Get the file
  6. }
  7. #Loop by index
  8. filenames.each_index{|i| #Loop through each index
  9.   localfile = "directory/file" + i.to_s + ".xls"
  10.   ftp.getbinaryfile(filenames[i],localfile)
  11. }
  12.  
Apr 15 '09 #4
On the Similar lines to Multiple file downloads, i am facing issue with deleting the files on remote FTP server.
<CODE>
ftp.delete("filename.xls") # This line works as i am giving exact filename
ftp.delete("*.xls") # Trying to delete all files with .xls extension , but i get 'getresp' : 550 Delete operation failed (Net : : FTPPermError) issue.
<CODE>
Wondering whether its an issue with permission, but it works according to my first statement with single file name.
How do i resolve such an error, or is there an issue in the way i hve written the code

cheers
May 4 '09 #5
improvcornartist
303 Expert 100+
Delete wants a filename, not an array. Loop through a list of files in the same way you did for getting them, but delete them this time.
May 4 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rajesh S | last post by:
Hi, Is it possible to upload multiple files using WebClient Class(Like HttpRequest Class).? If Possible please help!!
5
by: !!bogus | last post by:
Hi, I found this code in the MSDN library. It works fine for a single upload, but I can't figure out how to make it work for multiple file uploads...can someone please help me? Just paste it...
4
by: Pete Kane | last post by:
Hello all, how can I check for multiple file attributes using bitwise operators ? I'm thinking along the lines of string fname = "c:\\test.txt"; if(File.GetAttributes(fname) &...
2
by: Ole Hanson | last post by:
Hi It seems to be virtually impossible to perform a download of more than 2 files at the time from any website? The same limitation seems to be put on IE, if I'm using that as Client. I want...
3
by: Robert Scheer | last post by:
Hi. Is it possible to download multiple files at once? One of my pages needs to allow my users to download 6 files. Instead of one link for each file, I am trying to create just one link that...
1
by: Drew | last post by:
Hi all I'm fairly new to python so please forgive my lack of comprehension of the obvious. I'm writing a script to ftp files to a server. This script will run weekly. Part of the script first...
1
by: CodeMonkey | last post by:
Hi all, I have an ASP .NET 2.0 project with the following path on my dev machine: D:\Profiles\MyUsername\My Documents\Visual Studio 2005\Projects \abcWebSite2007\abcWebSite2007\Somefile.aspx ...
3
by: Swizylstik | last post by:
Can anyone guide me in how to create a document that will allow a user to choose via checkboxes which files they would like to download and then have the download occur concurrently? In other words,...
7
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.