Skip to content

Directory list items

The DirListItem type represents a file or directory entry returned by directory listing functions in SyncJS.

ts
type DirListItem = {
  Name:      string;    // see the note below: full path from a listing, bare name from a stat
  Type:      string;    // "FILE" or "DIR"
  Size:      number;    // File size in bytes
  TimeStamp: TimeStamp; // Timestamp of the item; see below
}

All functions that return directory lists provide an array of DirListItem objects.

IMPORTANT

Name is not always the same shape. ListDir and ListDirR set it to the fully qualified path, for example /var/inbox/report.csv. StatFileSystemObject sets it to the bare name, for example report.csv. Feeding one into code written for the other is an easy mistake; use ExtractName() and ExtractPath() to get whichever half you meant.

You can use standard JavaScript methods to iterate over the array and access each item's properties.

NOTE

TimeStamp is a value from the host process, not a JavaScript Date: it has no getTime() and no toISOString() of its own. Pass it to ToDate() to get a real Date, or straight to FormatDateTime() to get a formatted string. Both read it directly.

Examples

Example 1: Iterating with a for loop

ts
// Acquire directory list
var dirList = cli.ListDir('/docs');
if (Array.isArray(dirList)) {
  for (var i = 0; i < dirList.length; i++) {
    Log(dirList[i].Name);
  }
}

Example 2: Iterating with forEach

ts
// Acquire directory list
var dirList = cli.ListDir('/docs');
if (Array.isArray(dirList)) {
  dirList.forEach(function(item, index, array) {
    Log(item.Name + ' [' + item.Size + ' bytes] [' + item.Type + ']');
  });
}