Sample code for 30+ languages & platforms
JavaScript

Get Filenames in a Remote Directory

Gets the names of files in a remote FTP directory.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

var ftp = new CkFtp2();

ftp.Hostname = "ftp.example.com";
ftp.Username = "myLogin";
ftp.Password = "myPassword";
// Use explicit TLS
ftp.AuthTls = true;
ftp.Port = 21;

// Connect and login to the FTP server.
success = ftp.Connect();
if (success !== true) {
    console.log(ftp.LastErrorText);
    return;
}

// Iterate over .txt files.
ftp.ListPattern = "*.txt";
var n = ftp.GetDirCount();
console.log("n = " + n);

var i = 0;
while (i < n) {

    console.log(i + ": " + ftp.GetFilename(i));

    i = i+1;
}