function ListDir(directory, include)


The ListDir method retrieves the list of objects, typically files and other directories, contained inside the specified directory.
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 ListDir command should return, and can have either one of the following 3 values:

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


This function is NOT recursive, so only items that are contained inside directory will be returned. Items contained in sub-directories of directory will not be included in the results. For a recursive version of this function, please, look at ListDirR.


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.ListDir('/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.ListDir('/docs', FilesOnly);
  dirList.forEach(myFunction);
  function myFunction(item, index, array) {
    Log(item.Name + ' [' + item.Size + ' bytes] [' + item.Type + ']');
  }
  scli.Close();
 }
 scli = null
}