Download from FTP into a dated location


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. require 'net/ftp'
  2. require 'fileutils'
  3. include FileUtils
  4.  
  5. #creates dir
  6. cd( "/Users/user/Folder/" )
  7.  
  8. dirc = Time.now.strftime("%Y%B%d")
  9. Dir.mkdir( dirc , 0700 )
  10.  
  11. cd( "/Users/user/Folder/" + dirc)
  12.  
  13. ftp = Net::FTP.new('ftp.example.com')
  14. # run the script like
  15. # ruby ftp.rb username password
  16. ftp.login(user=ARGV[0], ARGV[1])
  17. files = ftp.chdir('/OUTGOING')
  18. files = ftp.nlst('*')
  19. puts "File list obtained... #{files}"
  20. files.each do |fname|
  21. puts "Downloading file #{fname}"
  22. ftp.getbinaryfile(fname, fname)
  23. # uncomment the next two lines if you want to delete the files after download
  24. #puts "Deleting file #{fname}"
  25. #ftp.delete(fname)
  26. end
  27. ftp.close

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.