Perl
Chilkat2 Perl
FTP Iterate over Files in Directory Matching ListPattern
See more RSA Examples
Uses the ListPattern property to iterate over the files in a directory matching the pattern.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $ftp = Chilkat2::Ftp2->new;
$ftp->Hostname('ftp.example.com');
$ftp->Username('my_login');
$ftp->Password('my_password');
$ftp->Port(21);
$ftp->AuthTls(1);
my $success = $ftp->Connect();
if (!$success) {
print $ftp->LastErrorText, "\n";
exit;
}
# Change to the "images" sub-directory located under our FTP account's home directory.
$success = $ftp->ChangeRemoteDir('images');
if (!$success) {
print $ftp->LastErrorText, "\n";
exit;
}
$ftp->ListPattern('*.png');
# Fetch the current remote directory contents by calling GetDirCount
my $n = $ftp->GetDirCount();
if ($n < 0) {
print $ftp->LastErrorText, "\n";
exit;
}
my $i = 0;
my $filename = '';
my $sbLocalPath = Chilkat2::StringBuilder->new;
while ($i < $n) {
$filename = $ftp->GetFilename($i);
print $filename, "\n";
# Download this file.
$sbLocalPath->SetString('qa_output/');
$sbLocalPath->Append($filename);
$success = $ftp->GetFile($filename, $sbLocalPath->GetAsString());
if (!$success) {
print $ftp->LastErrorText, "\n";
exit;
}
$i = $i + 1;
}