ListFiles (search for files)¶
function ListFiles(directory, mask: string) []DirListItem
The ListFiles
method retrieves the list of files (only files, not sub-directories) found 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 directory
parameter must specify an existing directory on the remote side.
The mask
parameter must specify to file-mask to match while searching, wildcards are supported (for example: *.docx).
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.ListFiles('/docs', '*.docx');
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.ListFiles('/docs', '*.docx');
dirList.forEach(myFunction);
function myFunction(item, index, array) {
Log(item.Name + ' [' + item.Size + ' bytes] [' + item.Type + ']');
}
cli.Close();
}
cli = null
}