Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi,

Here's how I might do it (if I understand you correctly):

#!/usr/bin/perl use strict; use warnings; use File::Copy; use File::Find; use File::Spec; my $source="your_source_dir"; my $dest="your_destination_dir"; my $abs_source=File::Spec->rel2abs($source); my $abs_dest=File::Spec->rel2abs($dest); print("Source directory: $abs_source\n". "Destination directory: $abs_dest\n"); sub fileprocess{ print("Processing file: $_ ($File::Find::name)\n"); my $rel_file=File::Spec->abs2rel($File::Find::name, $abs_source); my $dest_file=File::Spec->rel2abs($rel_file, $abs_dest); if (-d $_){ # deal with directories as special case mkdir $dest_file or die("$!\n"); }else{ # copy the files copy($File::Find::name, $dest_file) or die("$!\n"); # change the permissions to those of the source my $mode=(stat($File::Find::name))[2]; chmod($mode, $dest_file) or die("$!\n"); } } sub postprocess{ my $rel_dir=File::Spec->abs2rel($File::Find::dir, $abs_source); my $dest_dir=File::Spec->rel2abs($rel_dir, $abs_dest); # change the permissions to those of the source my $mode=(stat($File::Find::dir))[2]; chmod($mode, $dest_dir) or die("$!\n"); } find ({wanted => \&fileprocess, postprocess => \&postprocess}, $abs_source);
Hopefully this should also be moderately portable.

Update:Bug fix - the original code would fail to process files in a given directory if the user did not have write permission for that directory (which is what you want to do...duh, silly me). Fixed using the 'postprocess' feature of File::Find to adjust directory permissions after processing the files in that directory.

Good luck,
Tim

Update2: I just noticed that the perlmonks documentation on File::Find appears to be incomplete. Check out your local 'perldoc File::Find' or go here.


In reply to Re: copy a directory by tfrayner
in thread copy a direcory by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found