Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

filename matching caracters without pattern

Status
Not open for further replies.

eve25

Programmer
Feb 9, 2004
32
US
Hi folks,
I have to dowload files like the one with a .dat extension and having "qu" in their name (just an example)
I would have liked to write in my code someting like $ftp->get(*qu*.dat); I think I barely tried everything between *,.,/
I finally write a code using regular expressions, (see at the end) but it is a bit long and I ll have also to copy,move files I don't know the entire name.
Would you know an easier way to do it?

Thanks a lot and have a great WE!

*******************my code************************
# general subroutine with parts that can be commented out and add as arguments
# calling it: ftp(url,directory,pattern) // because anonymous connexion + no file to put in (pattern represents the cracteres we want the file to have in its name,pb:eek:nly one set of caracteres can be recognized or have to change the code depending on how many patterns we have...)
sub ftp
{
print "starting ftp subroutine\n";
my $url=$_[0];
my $directory=$_[1];
my $getFilename;
my $pattern=$_[2];
my $ftp= Net::FTP->new($url) or die "Can't connect: $@\n";
$ftp->login() or die "Could'nt login in anonymous\n";
my @list=$ftp->ls($directory) or die "Could'nt list directory $directory\n";#get the list of files in this remote directory
$ftp->cwd($directory) or die "Could'nt change directory into $directory\n";#change directory
foreach (@list)
{
my $prefix=$directory."/";#to have just the name of the file (and not the directory extention before)
$_=~/$prefix/;
$getFilename=$';
if ($getFilename=~ /$pattern/)
{
$ftp->get($getFilename) or die "Could'nt get $getFilename\n";
print "$getFilename downloaded\n";
}
$ftp->quit() or warn"Couldn't quit, oh well...";
print "done with ftp subroutine\n";
return @list;
}
 
Eve,

I would use the dir command which, if you call it in an array context, will return an array containing a list of the files in the current directory.

You can then create another array using grep() to pick out the files you want, and work through that array getting those files individually.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top