6

Using the following ftp_download method works, but if I change

ftp.getbinaryfile(file,localdir,1024)   #=> Saves the file to localdir

to

ftp.getbinaryfile(file)    #=> returns nil

I get nil returned. According to

http://www.ruby-doc.org/stdlib-2.0/libdoc/net/ftp/rdoc/Net/FTP.html#method-i-getbinaryfile

inilf I set localfile to nil as above, the data should be retrieved and returned by the method. What am I doing wrong?

def ftp_download(domain,remotedir,filename_regex,user=nil,pwd=nil)
  ftp = Net::FTP::new(domain)
  if user && pwd
    ftp.login(user, pwd)
  end
  ftp.chdir(remotedir)
  fileList = ftp.nlst(filename_regex)

  fileList.each do |file|
    localdir=File.join(remotedir,file)
    localdir=localdir[1..-1] if localdir[0]="/"
    FileUtils.mkdir_p(File.dirname(localdir))
    ftp.getbinaryfile(file,localdir,1024)
  end
  ftp.close
end
toro2k
  • 19,020
  • 7
  • 64
  • 71
user2012677
  • 5,465
  • 6
  • 51
  • 113

1 Answers1

11

If you look at the getbinaryfile method signature you will notice that the default value for the second parameter (localfile) is not nil but File.basename(remotefile)

getbinaryfile(remotefile, 
              localfile=File.basename(remotefile), 
              blocksize=DEFAULT_BLOCKSIZE)

If you want localfile to be nil you have to pass it explicitly:

ftp.getbinaryfile(file, nil)
toro2k
  • 19,020
  • 7
  • 64
  • 71
  • I have tried ftp.getbinaryfile(file, nil), and it seemed to return data now. thank you However, do you know why it would be so much slower? When I save a file down to a file, the file is saved in less than a second. If I used ftp.getbinaryfile(file,nil) it takes about 1 minute to return the data. – user2012677 May 08 '13 at 15:13
  • I've tried and I didn't experienced such a difference between writing to a file and just returning the string assigning the result to a variable, what do you do with the data returned? – toro2k May 09 '13 at 07:11
  • right now, nothing, just displaying the data in the irb. The file is large, about 30mb. Seems that if I add, "; nil" to suppress output, it gets faster. thanks! – user2012677 May 11 '13 at 16:38
  • 1
    @user2012677 "Just displaying the data in irb" is what take all that time. – toro2k May 11 '13 at 17:44