Sidebar

Can I set up a single channel to monitor multiple subdirectories of a single secure ftp location?

0 votes
497 views
asked Jun 20, 2017 by scott-b-8720 (560 points)

I'd like to be able to pull in all *.pdf files in any subdirectory where the files look something like this:

  • /ftpRoot
    • /sub1
    • /sub2
    • /sub3

If this cannot be done using the default sftp source node then does anyone have a good primer on initiating and browsing a secure ftp connection via script?

1 Answer

0 votes
 
Best answer

The standard FTP receiver can only monitor one directory.  However you can write a custom script to monitor multiple directories.  Here is an example:

var sftpHost = '{host}'; // change this to the FTP host that you want to connect too
var sftpPort = 22;  // change this to the port you want to connect too
var userName = '{username}'; // change this to the FTP username
var password = '{password}'; // change this to the FTP password

var directories = ["directoryA", "directoryB"];

// loop through each directory
for (var currentDirectory = 0; currentDirectory < directories.length; currentDirectory++) {
   // get count of files in current directory
   var fileCount = qie.countSFTPEndpoint(sftpHost, sftpPort, directories[currentDirectory], true, true, null, userName, password);
   qie.warn('Count in ' + directories[currentDirectory] + ': ' + fileCount);
   
   if (fileCount > 0) {
      // get filenames for each directory
      var fileNames = qie.listSFTPEndpoint(sftpHost, sftpPort, directories[currentDirectory], true, true, null, userName, password);
      
      // loop through each file and pull it down from the ftp server
      for (var currentFile = 0; currentFile < fileNames.length; currentFile++) {
         var filePath = directories[currentDirectory] + '/' + fileNames[currentFile];
         qie.warn('Retrieving File: ' + filePath);
         
         // this line will retrieve the file from the ftp server and then DELETE it
         var file = qie.readSFTPFile(sftpHost, sftpPort, filePath, true, null, true, true, null, userName, password, 60000);
         
         // extract file bytes from XML message that is returned (the XML message contains the file meta-data and the base64 encoded bytes
         var fileBytes = qie.base64DecodeToBytes(file.getNode('/ftpFile/encodedBytes'));
         
         // add file to inbound queue
         qie.addInboundMessage(fileBytes, fileNames[currentFile]);
      }
   }
}

answered Jun 20, 2017 by ben-s-7515 (12,320 points)
selected Jun 21, 2017 by sam-s-1510
...