Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

copy directories

by thealienz1 (Pilgrim)
on Mar 16, 2001 at 22:49 UTC ( #64973=perlquestion: print w/replies, xml ) Need Help??

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

Okay now I am really peeved off... I have been working on this freaking script for the past two days... I though i had it, but no there is something wron with. And I knoW.... I KNOW... I have no idea what it is.

What I've been trying to do is copy a directory structure to a new directory and copy the files along. Now I have done this before with no troubles, but for some odd reason in this script alone the mkpath doesn't work.

#Module use to recurse files in directory use File::Find; #Module used to copy files use File::Copy; #Module to copy directory structure use File::Path; if(defined($ARGV[0]) && defined($ARGV[1])) { $ARGV[0] =~ s/\\/\//g; $ARGV[1] =~ s/\\/\//g; print "$ARGV[0]\n\n"; #open data for user file open(DBUSER,"$ARGV[1]") || die "Cannot open user : $!"; my @Directory_Of_Users = <DBUSER>; close(DBUSER); foreach $Single_User_Directory (@Directory_Of_Users) { chomp($Single_User_Directory); $Single_User_Directory =~ s/\\/\//g; if(-f "$ARGV[0]") { #If its just a file print "Copying file to $Single_User_Directory - "; copy("$ARGV[0]","$Single_User_Directory"); print "Successful\n"; } elsif(-d "$ARGV[0]") { print "Copying path to $Single_User_Directory - "; mkpath([$ARGV[0], $Single_User_Directory], 1, 0711); print "Successful\n"; find(\&each_file,"$ARGV[0]//"); } else{ die "You Suck JT Archie - there's your motivation..."; } } } else{ print qq~ Usage: perl sendfile.pl <file(s)> <group list> <file(s)> - can either specify a single file or directory NOTE: if not full path to file then assumes in current directory <group list> - text file of directories to send to NOTE: if not full path assumes in current directory ~; } sub each_file{ my $filename = $_; my $filepath = $File::Find::name; if(-f "$filepath") { my $copypath = $filepath; $copypath =~ s/$ARGV[0]/$Single_User_Directory/i; copy("$filepath","$copypath"); } }

$ARGV[0] = a directory with no backslash and $ARGV[1] = a file to the directories I am to copy the "copy" directory to...

"The pajamas do not like to eat large carnivore toasters."
In German: "Die Pyjamas mögen nicht große Tiertoaster essen.
In Spanish: "Los pijamas no tienen gusto de comer las tostadoras grandes del carnívoro."

Replies are listed 'Best First'.
(jeffa) Re: copy directories
by jeffa (Bishop) on Mar 17, 2001 at 02:00 UTC
    Here's the deal - your are breaking all kinds of rules.
    Not the rules you are suppose to break either.

    USE STRICT!!

    Having said that, here is my interpretation of your code. I took the liberty of only allowing unix style paths in the list file - they do not contain trailing slashes.
    use strict; use IO::File; use File::Find; use File::Copy; &USAGE,exit unless my ($file,$list) = @ARGV; #open data for user file my $LIST = new IO::File($list) or die "Cannot open user : $!\n"; my @DIR = <$LIST> or die "List file is empty!\n"; $LIST->close; foreach my $target (@DIR) { chomp $target; find sub { return if /^\./; if (-f) { print "cp $_ $target/$_\n"; #copy($_,"$target/$_"); } elsif (-d) { print "mkdir $target/$_\n"; #mkdir("$target/$_",0711); } }, $file; } sub USAGE { print "Usage: perl sendfile.pl <file(s)> <group list>\n" }
    But alas, it does not work. The problem is remembing what path to copy to - File::Find will handle recursing into the source directory, but how do you maintain a synchronized recursing into the target directory?

    So, since I can't solve your problem I will re-ask the monks:

    What is the best way to mirror a directory tree using Perl?

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
(tye)Re: copy directories
by tye (Sage) on Mar 16, 2001 at 23:13 UTC

    You don't tell us how this is failing. Some details about that would make getting an answer much more likely.

    You use "$var" a lot when $var would suffice. I seriously doubt this is causing you problems here (it should only cause problem when $var contains a complex value such as a reference). But you might want to try to break yourself of that habit.

            - tye (but my friends call me "Tye")

      Its more the mkpath causing me troubles...

      "The pajamas do not like to eat large carnivore toasters."
      In German: "Die Pyjamas mögen nicht große Tiertoaster essen.
      In Spanish: "Los pijamas no tienen gusto de comer las tostadoras grandes del carnívoro."

Re: copy directories (Offtopic?)
by McD (Chaplain) on Mar 17, 2001 at 00:31 UTC
    Not a Perl soloution, but probably easier than what you've got here, if you have the luxury of a unix platform:

    (cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xvfp -) From the Linux Tips-HOWTO, 2.6, attributed to Alan Cox.

    Peace,
    -McD

Re: copy directories
by arturo (Vicar) on Mar 17, 2001 at 00:01 UTC

    mkpath returns a list of the directories it's created. So check the return value of that call, that should help you debug.

    my @newdirs = mkpath (blah blah blah) or die "mkpath failed: $!\n"; # you can now do something with @newdirs should you need to, such as p +rint it out

    HTH

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      For the darnest reason that doesn't work... I am using $ARGV could that have anything to do with it?

      "The pajamas do not like to eat large carnivore toasters."
      In German: "Die Pyjamas mögen nicht große Tiertoaster essen.
      In Spanish: "Los pijamas no tienen gusto de comer las tostadoras grandes del carnívoro."

        Please, "doesn't work" how? The call goes through without dying? Did you print out @newdirs ?

        You need to be more verbose in your descriptions.

        (and I don't think the use of @ARGV has anything to do with it, fwiw)

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2019-10-21 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?