Stat (get object metadata)

function Stat(remoteObj: string) StatObject

StatObject = {
  Valid:  boolean
  Exists: boolean
  Stat:   DirListItem
}

Every client object has a method called Stat which tries to retrieve information about a remote object (file or directory) and, in doing so, checks whether such object exists or not.

A call to the Stat method will always return a StatObject JSON object, which - in turn - contains the file/directory information (metadata) in the form of a DirListItem sub-object (if the file/directory exists, of course).

So, for example, if we invoke Stat on a file, and such file exists, we will receive in return an object that more or less looks like this:

{
  Valid: true,
  Exists: true,
  Stat: {
    Name: "/docs/budget.xlsx",
    Type: "FILE",
    Size: 526546,
    TimeStamp: 1576095950
  }
}

Whereas, if the file does not exist, we will receive an object that looks like this:

{
  Valid: true,
  Exists: false,
  Stat: {}
}

Attention

Valid is true when the Stat command has returned valid information. This does not mean that the file/directory exists, only that the Stat command was accepted by the remote side and executed without issues.

Example:

{
  var cli = new SftpClient();
  cli.Host = 'your.sftpserver.com:22';
  cli.User = 'someusername';
  cli.KeyFile = './my_id.rsa';
  if (cli.Connect()) {
    // ...
    var res = cli.Stat('/docs/budget.xlsx');
    if ((res.Valid) && (res.Exists)) {
      Log('The file exists and its size is ' + res.Stat.Size);
    }
    // ...
    cli.Close();
  }
  cli = null
}