Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Compare the contents of directory with the contents of file

by repson (Chaplain)
on Feb 24, 2001 at 06:55 UTC ( #60600=note: print w/ replies, xml ) Need Help??


in reply to Compare the contents of directory with the contents of file

I guess you might be storing the list of contents of a directory in a flat file, and want to check if the current contents differ from the stored record.

In that case I would build a hash of the lines of the file like so:

%files = map { tr/\015\012//d; ($_,1); } (<FILE>);

Where FILE is an already open filehandle.
Then I would loop over the directory like so (where DIR comes from opendir):

while (defined($_ = readdir(DIR))) { next if /^\.\.?$/; unless (delete $files{$_}) { print "New: $_\n"; } }
Reporting things that weren't in the file and removing the others from the hash.
And finally reporting thing from the file that are no longer in the directory:
for (keys %files) { print "Gone: $_\n"; }
Of course if you are not asking for that, then your question needs clarification.

2001-03-04 Edit by Corion : Removed an erroneous <CODE> tag


Comment on Re: Compare the contents of directory with the contents of file
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2015-07-04 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls