ListDir (listing a directory)

function ListDir(directory: string, include: InclusionClause) []DirListItem

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

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

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

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

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