namishtiwari has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Here is my piece of code--
#!/usr/bin/perl use Net::FTP; use Archive::Zip; use IO::File ; print "Retrieving file from log.abc.com...\n"; $loginip='123.678.90.18'; $loginid='nkt123'; $loginpaswd='12345678'; ($sec,$min,$hour,$mday,$mon,$year)=(localtime(time))[0,1,2,3,4,5]; $time_stamp= "_" . (1900+$year) . "_" . ($mon+1) . "_" . ($mday) . "_" + . $hour . "_" . $min . "_". $sec; printf "time stamp = $time_stamp\n"; unless(-d "temp") { mkdir("temp"); } if( -d "temp") { chdir("temp"); } $ftp = Net::FTP->new(($loginip), Debug => 0) or die "Cannot connect to log.abc.com: $@ \n"; $ftp->login($loginid,$loginpaswd) or die "Cannot login ", $ftp->message; $source_dir="/nt/logs/nali05/"; $ftp->cwd($source_dir) or die "Cannot change working directory ", $ftp->message; $ftp->binary || die "Unable to set mode to binary. ", $ftp->message; @list=$ftp->ls(); printf "list = \n"; print @list; foreach $file (@list) { $ftp->get($file) or die "get failed ", $ftp->message; my $zipname = $file; my $destinationDirectory = 'C:\Perl Script\temp'; my $zip = Archive::Zip->new($zipname); foreach my $member ($zip->members) { next if $member->isDirectory; (my $extractName = $member->fileName) =~ s{.*/}{}; $member->extractToFileNamed( "$destinationDirectory/$extractName"); } #rename($file,"${file}_${time_stamp}"); $ftp->delete ($file) or die "rm -rf failed ", $ftp->message; } #} $ftp->quit;
I am getting the files from ftp site and the files that are zipped ones. I am able to extract them also in the same folder. I need little help in--- The unzipped files which i got, i only need few of them not all. The format of the filename is like this---
risk_14Jul09_16_10_48.log web_14Jul09_16_10_48.log
I get so many files files when i unzip thme , this is just a sample. In these two files i want the files which contains web in that and delete the rest from the folder. And one more help i need is this-- We get 4-5 log files for a day with the fromat above. There will be files older than today also, i want to run my script for today's logs only. Thanks NT

Replies are listed 'Best First'.
Re: chosse selected files and delete rest
by jrsimmon (Hermit) on Jul 28, 2009 at 15:02 UTC

    Untested

    #keep based on pattern match opendir(DIR, $destinationDirectory) or die $!; while(my $file = readdir(DIR)){ next if -d $file;#skip directory entries unlink unless $file =~ /$pattern/; }
    #keep based on a prepopulated hash %prepopulatedHash =(FILE1 => 1, FILE2 => 1, FILEN => 1); opendir(DIR, $destinationDirectory) or die $!; while(my $file = readdir(DIR)){ next if -d $file;#skip directory entries unlink unless exists($prepopulatedKeepHash{$file}; }
      ...or, for your pattern match match based solution, why not utilize the fact that unlink takes a list viz (also untested:-):
      unlink grep { -f $_ && ! /$pattern/ } glob "$Directory/*";

      A user level that continues to overstate my experience :-))