ListFilesR (recursive file search)¶
function ListFilesR(directory, mask: string) []DirListItem
The ListFilesR
method retrieves the list of files (only files, not sub-directories) found inside the specified directory and all of its sub-directories (this function is recursive).
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.ListFilesR('/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.ListFilesR('/docs', '*.docx');
dirList.forEach(myFunction);
function myFunction(item, index, array) {
Log(item.Name + ' [' + item.Size + ' bytes] [' + item.Type + ']');
}
cli.Close();
}
cli = null
}