Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Hash of directories?

by phathead22 (Novice)
on Jan 26, 2001 at 05:18 UTC ( [id://54455]=perlquestion: print w/replies, xml ) Need Help??

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

I'm creating a backup routine; I need to read in a directory of directories,
look for dissimilar dir's, then write the unique ones to the target dir.
I've gotten most of the comm stuff done, but need to be able to compare either 2 hashs,
(source to target dir's) or compare one hash to the existing directory structure.
Suggestions? Thanks!

Replies are listed 'Best First'.
Re: Hash of directories?
by jynx (Priest) on Jan 26, 2001 at 06:57 UTC

    If you really plan on doing this in Perl your best bet would be to use the File utilities (File::Find is a fantastic thing).

    However, i'd be wary of doing a backup script in perl depending on your platform. Maybe you could help us out by telling us what platform you're running on, why you chose to use perl to do this as opposed to known utilities (say tar or dump on unix) and other such useful inmformation so an answer would be a bit easier to produce...

    jynx

    ps This isn't meant as bad karma, just need some more info
    pps And of course there's merlyn's recent post on homework that comes to mind...

      This is an NT based system running Active State Perl; I'll admit that
      it was a pain getting some of the modules/libraries going (there is a recent
      question RE the same:) I am not compressing any data, or even tar'ring. I just want to copy unique files
      from source to target. Seems to me that the Rubbish Lister was intended for this!
      Thx PH
        ok,

        You're already building a hash of the directory structure you said, so along that vein how about this (untested piece of code)

        use File::Find; # these should work on any platform : ) use File::Copy; # i'm naming your target directory hash %files, # and the directories are $target and $source (strings) sub checker { return if exists %files, $_; copy "$target$_", $File::Find::name; } find {wanted => \&checker}, $source;
        This is the first code that pops to mind. It doesn't handle directories inside of directories well at all, but if you take a look at the File utilities you should be able to get a good feel for what it's lacking. Also, for testing if a file is the same as another, you could use File::stat which would be much more specific than just names of files. And finally, for creating the target hash structure to begin with you might want to use File::Find to help you traverse correctly down the tree. That way you don't get any infinite directories or some such nasty beasts.

        find and copy do all of the grunt work here, you only need to modify the code to deal with how you laid out your hashes. It is untested, but it should work for directories that don't have any subdirectories in them.

        Hope That Helps,
        jynx

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found