Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

ignore list of files using readdir function

by kaka_2 (Acolyte)
on Jul 22, 2013 at 10:32 UTC ( #1045619=perlquestion: print w/ replies, xml ) Need Help??
kaka_2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Experts,

i have to list out all the files from a directory, but i also want to exclude files which has _ACK_ in it. I have created simple sub as below which can list all the files but i am not getting how can i exclude

sub GetDirFiles { my ($path) = @_; opendir DIR, $path or die $!; my @files = readdir DIR; closedir DIR; return(@files); }

is it possible? if yes. could someone please help me with this

Comment on ignore list of files using readdir function
Download Code
Re: ignore list of files using readdir function
by kcott (Abbot) on Jul 22, 2013 at 10:36 UTC

    G'day kaka_2,

    The first example in the readdir documentation shows how to do this.

    -- Ken

Re: ignore list of files using readdir function (grep filter)
by Anonymous Monk on Jul 22, 2013 at 10:38 UTC
      Put what you want to exclude or include into a file (eg. file1) one per line... Put the output of the directory contents into file2...
      grep -i -v -f file1 file2
      similarly if you wish to "match" everthing in file1 leave off the -v
      grep -i -f file1 file2
      the -i ignores case so leave that off if you wish an exact match (case sensitive)
      grep -f file1 file2
      the first file after the -f is always the "from" file

      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-

        So i was thinking is it possible to list files which are between current time to current time - 15 minute?

        Yes , of course it is possible, see stat and time

Re: ignore list of files using readdir function
by mtmcc (Hermit) on Jul 22, 2013 at 10:53 UTC
    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!).

      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?
        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...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-08-01 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls