Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^3: Searching for files efficiently help!

by aaron_baugher (Curate)
on Nov 16, 2011 at 22:22 UTC ( #938480=note: print w/replies, xml ) Need Help??

in reply to Re^2: Searching for files efficiently help!
in thread Searching for files efficiently help!

There would seem to be two ways you can do this (but see correction below):

  1. Go through your array, deleting each file name if it exists as a file, or
  2. Go through your directory structure, checking each filename/path against your array (after turning it into a hash), and deleting it if it exists in the hash.

Generally, I would prefer the first method. It's almost certain to be faster to go through a list of files and check for their existence than to traverse an entire directory structure and check every file against a list. If you simply go through your array, checking for the existence of each pathname and deleting if it's found, then it doesn't matter how large or complex your directory structure is.

for my $file (@files){ if( -f $file ){ report($file); # however you want to report a match if( unlink $file ){ print "Deleted $file\n"; } else { warn "Unable to delete $file\n"; } } }

Correction: As Jethro pointed out, I misunderstood the original requirements, getting the two arrays he mentioned mixed up. The array he wants to check the files against does not have full path names, so my solution won't work. He will have to recurse through the directory structure and check them one by one.

Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.

Replies are listed 'Best First'.
Re^4: Searching for files efficiently help!
by jethro (Monsignor) on Nov 17, 2011 at 13:20 UTC

    Your script would only work if the filenames he is looking for came with complete path. Seems not to be the case

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938480]
[LanX]: hippo: I'm telling him constantly to upgrade to postgress...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2018-03-19 09:37 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (238 votes). Check out past polls.