Connecting Tech Pros Worldwide Help | Site Map

FTP - Multiple file downloads using ruby

  #1  
Old April 15th, 2009, 08:31 AM
Newbie
 
Join Date: Jan 2008
Posts: 17
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

Last edited by eWish; April 28th, 2009 at 01:55 PM. Reason: Added code tags
  #2  
Old April 15th, 2009, 03:32 PM
Expert
 
Join Date: May 2007
Posts: 213

re: FTP - Multiple file downloads using ruby


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.
  #3  
Old April 15th, 2009, 06:35 PM
Newbie
 
Join Date: Jan 2008
Posts: 17

re: FTP - Multiple file downloads using ruby


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!!
  #4  
Old April 15th, 2009, 09:25 PM
Expert
 
Join Date: May 2007
Posts: 213

re: FTP - Multiple file downloads using ruby


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.  
  #5  
Old May 4th, 2009, 12:23 PM
Newbie
 
Join Date: Jan 2008
Posts: 17

re: FTP - Multiple file downloads using ruby


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
  #6  
Old May 4th, 2009, 04:25 PM
Expert
 
Join Date: May 2007
Posts: 213

re: FTP - Multiple file downloads using ruby


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