Search code examples
node.jsssh2-sftp

ssh2-sftp-client getting same file over and over


Im trying to get a bunch 2k files from a Sftp server using node and ssh2-sftp-client.

ftp.connect({
            host: '...',
            port: '...',
            username: '...',
            password: '...',
            readyTimeout: ...
        }).then(() => {
            return ftp.list('/Stmts/')
        }) .then((data) => {
            let filesToTransfer = [];
            //only get files I dont already have
            for (let i = data.length-10; i < data.length; i++) {
                if (!blobs.includes(data[i].name)) {
                     filesToTransfer.push(data[i].name)
                }
            } 
            // Get the files in the filesToTransfer Array
            for (const file of filesToTransfer){
               ftp.fastGet('/Stmts/' + file, 
               path.join(__dirname, '../Files/' + file))
            }

This successfully Gets a file in the array and names it correctly except that every file actually the same file downloaded every time.

Thanks


Solution

  • solved this problem by doing this:

    .then((data)=>{
     var x = 0;
            var loop = function (arr) {
                let file = arr[x].name;
                let remoteFileNameWithPath = '/Stmts/' + file;
                let localFilePath = path.join(__dirname, '../Files/' + file)
                ftp.fastGet(remoteFileNameWithPath, localFilePath).then((a) => {                    
                    x++
                    if (x < arr.length) {
                        loop(arr)
                    } 
                })
            }
     })
    loop(data)