Skip to content

Instantly share code, notes, and snippets.

@kaleguy
Created February 4, 2017 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaleguy/cb66ddb2211615b553dc44a472aefe69 to your computer and use it in GitHub Desktop.
Save kaleguy/cb66ddb2211615b553dc44a472aefe69 to your computer and use it in GitHub Desktop.
Download CSV files via FTP
Client = require 'ftp'
async = require 'async'
remote_path ='/public_html/targetfolder/'
local_path = 'data/'
fs = require 'fs'
c = new Client()
c.on 'ready', ()->
c.list remote_path, (err, list)->
if (err) then console.error 'list', err
file_funcs = (getFileFunc file.name for file in list)
async.series file_funcs, ()-> c.end()
getFileFunc = (name) ->
(callback)->
if /csv$/.test name
c.get remote_path + name, (err, stream)->
console.log 'Getting ', name
if err then console.log 'get', err
stream.once 'close', callback
stream.pipe fs.createWriteStream local_path + name
else
console.log "Skipping", name
callback()
c.connect(
host : 'ftp.targethost.com'
user : 'username@domain.com'
password : 'password'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment