FTP - Multiple file downloads using ruby

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idealfellow
    New Member
    • Jan 2008
    • 21

    FTP - Multiple file downloads using ruby

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

    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
    Last edited by eWish; Apr 28 '09, 12:55 PM. Reason: Added code tags
  • improvcornartist
    Recognized Expert Contributor
    • May 2007
    • 303

    #2
    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.

    Comment

    • idealfellow
      New Member
      • Jan 2008
      • 21

      #3
      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!!

      Comment

      • improvcornartist
        Recognized Expert Contributor
        • May 2007
        • 303

        #4
        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.
        Code:
        ftp.chdir(directory)
        filenames = ftp.nlst('*.xls') #This is an array of all Excel files in the ftp directory
        #Loop by value
        filenames.each{|filename| #Loop through each element of the array
          ftp.getbinaryfile(filename,filename) #Get the file
        }
        #Loop by index
        filenames.each_index{|i| #Loop through each index
          localfile = "directory/file" + i.to_s + ".xls"
          ftp.getbinaryfile(filenames[i],localfile)
        }

        Comment

        • idealfellow
          New Member
          • Jan 2008
          • 21

          #5
          On the Similar lines to Multiple file downloads, i am facing issue with deleting the files on remote FTP server.
          <CODE>
          ftp.delete("fil ename.xls") # This line works as i am giving exact filename
          ftp.delete("*.x ls") # 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

          Comment

          • improvcornartist
            Recognized Expert Contributor
            • May 2007
            • 303

            #6
            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.

            Comment

            Working...