How to implement a for-loop for an SFTP-Server

1.I am setting up an SFTP-Server with Nodejs with several functions implemented. (Read, delete, download…)

The problem is, that the server only sees the content of the first file in the directory he reads. Meaning while I can see all files lined up with the help of my WinSCP Client, it only opens the content of the first file. When I click on the second file, it shows the content of the first again, although the file is named different and so on. When I try to download the files, same happens; you can download each file, but as soon as you open them there is the content of the very first file in my directory. I know that it has to do with the for-Loop I wasn’t able to implement for a while now. I hope you guys can help with some experience/ expertise.

I used dummy user names and Paths for this website.

session.on("readdir", function(files, responder) {
var i = 0;
          responder.on("dir", function() {
            **if (results[i])** {
              console.warn(now + " Returning file: " + results[i]);
              responder.file(results[i]);
              **return i++;**
            } else {
            return responder.end();
            } 
          });
          return responder.on("end", function() {
            return console.warn(now + " Directory has been read");
          });
        });

session.on('stat', function(path, statkind, statresponder) {
      statresponder.is_file();             
      statresponder.permissions = 0o777; 
      statresponder.uid = 1;             
      statresponder.gid = 1;                   
      statresponder.size = 1234;        
   });

session.on("readfile", function(path, writestream) {
      var i = 0;
      **if(results[i])** {
        console.log(now + " Reading file: " **+ results[i]);**
        return fs.createReadStream("KKK_Files/"+results[i]).pipe(writestream); 
     }
      else { 
       console.warn(now + " no file to read");
      }
    }) 

session.on("delete", function(path, writestream) {
session.sftp(function(err, sftp) {
      if (err) throw err;
      var i = 0;
      fs.unlink("KKK_Files/" **+ results[i],** function(err){
         if ( err ) {
             console.log( "Error, problem starting SFTP: %s", err );
                 }
                 else
                 {
                  console.log(now + " all Files have been deleted");
                 }
             });
          });

        session.on("download", function(path, fastGet) {
            var moveTo = "C:/Users/User/Downloads/file.DAT";
            if (err) throw err;
            var i = 0;
                 fs.fastGet("KKK_Files/" + 
                        **results[i],**moveTo,function         
                 (err) {if ( err ) {
                  console.log("Error downloading the file", err);
                }
                else
                {
                console.log(now + "Successfully downloaded");
                }
              });
            });
             files.forEach(files => {
          results.push(files);
        });
      });
    });
 });
  1. What didn’t work:
for**(i=0; i<results.lenth; i++)** {
        console.log(now + " Reading file: " + results[i]);
        return fs.createReadStream("KKK_Files/" + results[i]).pipe(writestream); 
    }if (err) throw err;

I’m assuming that is just a typo when posting to the forum @vrtl1?

1 Like