The Ftp2.NumFilesAndDirs property returns the count of files and sub-directories in the current remote FTP directory, according to the ListPattern property. For example, if ListPattern is set to "*.xml", then NumFilesAndDirs returns the count of XML files in the remote directory.
The 1st time it is accessed, the component will (behind the scenes) fetch the directory listing from the FTP server. This information is cached in the component until (1) the current remote directory is changed, or (2) the ListPattern is changed, or (3) the ClearDirCache method is called.
The problem with the NumFilesAndDirs property is that it may return -1 if the behind-the-scenes directory listing download fails. Therefore, using it in a FOR loop like this is a bad idea:
for (i = 0; i <= ftp.NumFilesAndDirs - 1; i++) {
...
}
It’s better to do this:
int n = ftp.NumFilesAndDirs;
if (n < 0) {
// Display the ftp.LastErrorText and return..
} else if (n > 0) {
for (i = 0; i <= ftp.NumFilesAndDirs - 1; i++) {
...
}
}