Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: ignore list of files using readdir function

by mtmcc (Hermit)
on Jul 22, 2013 at 10:53 UTC ( #1045624=note: print w/ replies, xml ) Need Help??


in reply to ignore list of files using readdir function

Something like this should work:

sub GetDirFiles { my @files; my ($path) = @_; opendir (my $directory, $path) or die $!; while(readdir $directory) { push (@files, $_) unless $_ =~ m/_ACK_/; } closedir $directory; return(@files); }

EDIT: Declare my @files (thank you hdb!).


Comment on Re: ignore list of files using readdir function
Download Code
Re^2: ignore list of files using readdir function
by kaka_2 (Acolyte) on Jul 23, 2013 at 08:00 UTC

    Hello There,

    I was able to use the grep in order to ignore the files with pattern

    sub GetINDirFiles { my ($path) = @_; opendir DIR, $path or die $!; my @files = readdir DIR; my @files = grep {!/\_ACK_/} readdir DIR; closedir DIR; return(@files); }

    now the problem i have is that there are around 5000 files and later when i compare these files agains another list of files, it takes too much time. So i was thinking is it possible to list files which are between current time to current time - 15 minute?

    Thank You. -KAKA-

      It shouldn't take 15 minutes to compare lists of filenames. What exactly are you trying to do?

        You are right, it takes almost 15 minute which is too much

        below is complete code.
        #! /usr/bin/perl use strict; use Math::BigFloat; Math::BigFloat->precision(0); sub GetINDirFiles { my ($path) = @_; opendir DIR, $path or die $!; my @files = readdir DIR; my @files = grep {!/\_ACK_/} readdir DIR; closedir DIR; return(@files); } sub GetOUTDirFiles { my ($path) = @_; opendir DIR, $path or die $!; my @files = readdir DIR; my @files = grep {/\_ACK.xml$/} readdir DIR; closedir DIR; return(@files); } # Main my $inpath = "/AAA/BBB/CCC/IN"; my $outpath = "/AAA/BBB/CCC/OUT"; my $outsuffix = "_ACK.xml"; my $insuffix = ".xml"; # Added by me my $timethreshold = 900; # set time threshold in seconds (900 se +conds equal 15 minutes) my @delindex; my @infiles = &GetINDirFiles($inpath); my @outfiles = &GetOUTDirFiles($outpath); my $index = 0; # index used to get string position in array foreach my $infile (@infiles) { $infile =~ s/(.*)$insuffix/$1/g; # remove suffix to do co +mparation # Added by me foreach my $outfile (@outfiles) { $outfile =~ s/(.*)$outsuffix/$1/g; # remove suffix t +o do comparation if ($outfile eq $infile){ push (@delindex, $index); # get list of st +rings to be removed from array } } $index += 1; } delete @infiles[@delindex]; # remove strings my $currenttime = time; # get current time from system (epoch t +ime) foreach my $file (@infiles) { next unless (-f "$inpath/$file$insuffix"); # ignore directo +ries # INSERT SUFFIX AGAIN ($insuffix) my $mtime = (stat "$inpath/$file$insuffix" )[9]; # get mt +ime from file (epoch time) # INSERT SUFFIX AGAIN ($insuffix) my $diff = ($currenttime - $mtime); if ($diff > $timethreshold) { print "\n - file " . $file . $insuffix . " in " . $inpa +th . " directory was created at more than " . Math::BigFloat->new($d +iff / 60) . " minutes."; # INSERT SUFFIX AGAIN ($insuffix) # PUT THE ACTION THAT YOU WANT DO HERE!!! } }

        i need to do it on regular interval like 5 minute or 15 minute, using an tool i use. so it really does not make sense if i check the files which i have already checked and i would not mind if this completes in minute or less then this but 15 minute is really too much

        kindly assist

        -KAKA-

      The first
      my @files = readdir DIR;
      in your code is superfluous. You are doing the work (at least the reading of the directory) twice. Might not account for the whole excess time, but maybe...

        Sorry. but that is commented, i dont know where the # gone

        .  # my @files = readdir DIR;

        -KAKA-

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1045624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-21 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls