How to get files matching a pattern using perl Net::SFTP::Foreign

This is my first submitted question at all and I hope someone can help me with this. I never used perl before so any other tips are highly appreciated too.

Lets say I have a perl script with NET::FTP that matches a pattern which can be specified while calling the script. For example:

perl ftp.pl '*.txt'

To keep it absolute simple, here is what i would do with NET::FTP after establishing the connection to the FTP-Server.

$Pattern = '*.*';
$ftp->cwd($FolderAtHost);
my @Files = $ftp->ls($Pattern);
foreach my $File (@Files)
{
     ...
}

Now I want to make the same thing with NET::SFTP. but the ls Method is completely different. It returns a reference to an Array instead of an array, which is not a big deal. But I don't find a way to check for a specific file-pattern like: *.*

I know there is a wanted => option in the ls method of the sftp package, but it requires RegEx instead of a file-pattern.

sftp->setcwd($FolderAtHost)
my @FilesRef = $sftp->ls(names_only => 1, wanted => qr/.../);
foreach my $File (@$FilesRef)
{
    ...
}

Do I have to write a File-Pattern to Regex converter first or is there a simpler way to achieve this? Maybe matching the filename within the foreach?

Upvotes: 2

Views: 1268

Answers (3)

salva
salva

Reputation: 10242

Net::SFTP::Foreign provides the glob method just for that:

sftp->setcwd($folder_at_host);
my $files = $sftp->glob('*.jpg');
for my $file (@$files) {
    ...
}

Upvotes: 2

It's working fine now. Thanks to JGNI and the Text::Glob module

after using the module:

use Text::Glob 'glob_to_regex';

we can use a Pattern like in ftp->ls:

$Pattern = *.*;
my $FilesRef = $sftp->ls(names_only => 1, wanted => glob_to_regex($Pattern));

Upvotes: 0

JGNI
JGNI

Reputation: 4013

Try using Text::Glob especially the glob_to_regex function in it

Upvotes: 1

Related Questions