function ListDirR(directory, include)


The ListDirR method retrieves the list of objects, typically files and other directories, contained inside the specified directory, and all of its sub-directories.
The result of this function is returned as a JavaScript array of objects. Each object is of DirListItem type.

Once a resulting array has been obtained, you can use the typical JavaScript ways to iterate over it, and check the various property of each one of its items.


The include parameter is used to determine what the ListDirR command should return, and can have either one of the following 3 values:

  • All: all files and sub-directories inside directory (and inside its sub-directories) will be returned
  • FilesOnly: only files contained inside directory (and inside its sub-directories)will be returned
  • DirsOnly: only sub-directories inside directory (and inside its sub-directories)will be returned


This function is recursive, therefore it will return all matching items from directory as well as from all of directory's sub-directories.


Example 1 (one way to iterate over a directory list, using a for cycle):

{
 var scli = new SftpClient();
 scli.Host = 'your.sftpserver.com:22';
 scli.User = 'someusername';
 scli.KeyFile = './my_id.rsa';
 if (scli.Connect()) {
  dirList = scli.ListDirR('/docs', All);
  for (var i = 0; i < dirList.length; i++) {
   Log(dirList[i].Name);
  }
  scli.Close();
 }
 scli = null
}


Example 2 (a different way to iterate over a directory list, using forEach):

{
 var scli = new SftpClient();
 scli.Host = 'your.sftpserver.com:22';
 scli.User = 'someusername';
 scli.KeyFile = './my_id.rsa';
 if (scli.Connect()) {
  dirList = scli.ListDirR('/docs', FilesOnly);
  dirList.forEach(myFunction);
  function myFunction(item, index, array) {
    Log(item.Name + ' [' + item.Size + ' bytes] [' + item.Type + ']');
  }
  scli.Close();
 }
 scli = null
}