avik1612 has asked for the wisdom of the Perl Monks concerning the following question:
This node falls below the community's threshold of quality. You may see it by logging in.
Re: Copy folders
by Samy_rio (Vicar) on Aug 28, 2007 at 07:21 UTC
|
Hi avik1612, copy the folder using File::Copy::Recursive
use strict;
use warnings;
use File::Copy::Recursive qw(dircopy);
dircopy('E:\test\split\Output','C:\New') or die("$!\n");
Regards, Velusamy R. eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Copy folders
by andreas1234567 (Vicar) on Aug 28, 2007 at 07:14 UTC
|
use strict;
use warnings;
use File::Copy;
use File::Find;
my @fromdirs = qw (/tmp/foo);
my $todir = q{/tmp/bar};
find(
sub {
if (-f) {
print "$File::Find::name -> $todir";
copy($File::Find::name, $todir)
or die(q{copy failed:} . $!);
}
},
@fromdirs
);
__END__
$ mkdir -p /tmp/foo
$ mkdir -p /tmp/bar
$ touch /tmp/foo/1
$ touch /tmp/foo/2
$ perl -l 635503.pl
/tmp/foo/1 -> /tmp/bar
/tmp/foo/2 -> /tmp/bar
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Copy folders
by moritz (Cardinal) on Aug 28, 2007 at 06:59 UTC
|
"not working" is not an error message.
Did the program compile? Did you get any error message? If the copy failed, did you print out $! for more information?
Generally File::Copy works, very likely the problem is on your side.
As andreas1234567 pointed out File::Copy is not suitable, but dircopy in File::Copy::Recursive does
what you want
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Copy folders
by SM177Y (Initiate) on Sep 13, 2015 at 12:57 UTC
|
Here is a pure perl method I made up after endless searching and having people tell me to use modules. This is such a simple concept, a required external module seems pretty lame. I have also made a recursive directory deleter so I/You can eliminate the need for File::Path qw(remove_tree) and File::Copy::Recursive qw(rcopy) altogether. If you use these together you can easily conduct a dir_move. And finally I included an fmove(); (you could use rename for certain instances obviously and fmove is identical to fcopy except for a final unlink) but if you were to combine fmove() into dircopy you could make a single sub/function to move a folder in one shot instead of recursive copy and then recursive delete.
#!/usr/bin/env perl
use strict; use warnings;
dircopy($ARGV[0],$ARGV[1]);
sub dircopy {
my @dirlist=($_[0]);
my @dircopy=($_[1]);
until (scalar(@dirlist)==0) {
mkdir "$dircopy[0]";
opendir my($dh),$dirlist[0];
my @filelist=grep {!/^\.\.?$/} readdir $dh;
for my $i (0..scalar(@filelist)-1) {
if ( -f "$dirlist[0]/$filelist[$i]" ) {
fcopy("$dirlist[0]/$filelist[$i]","$dircopy[0]/$filelist[$
+i]");
}
if ( -d "$dirlist[0]/$filelist[$i]" ) {
push @dirlist,"$dirlist[0]/$filelist[$i]";
push @dircopy,"$dircopy[0]/$filelist[$i]";
}
}
closedir $dh;
shift @dirlist;shift @dircopy;
}
}
sub fcopy {
my ($i,$data,$cpo,$cpn);
open($cpo,"<",$_[0]) or die $!; binmode($cpo);
open($cpn,">",$_[1]) or die $!; binmode($cpn);
while (($i=sysread $cpo,$data,4096)!=0){print $cpn $data};
close($cpn);close($cpo);
}
And here is the recursive directory deleter...
#!/usr/bin/env perl
use strict; use warnings;
dir_del($ARGV[0]);
sub dir_del {
my @dirlist=("$_[0]");
my $r=0; my $s=scalar(@dirlist);
while ( $r < $s ) {
opendir my($dh),$dirlist[$r];
my @filelist=grep {!/^\.\.?$/} readdir $dh;
for my $i ( 0..scalar(@filelist)-1 ) {
if ( -f "$dirlist[$r]/$filelist[$i]" ) {
unlink("$dirlist[$r]/$filelist[$i]");
} else {
push @dirlist,"$dirlist[$r]/$filelist[$i]";
$s=scalar(@dirlist);
}
}
closedir $dh;$r+=1;
}
my $a=scalar(@dirlist)-1;
for my $i ( 0..scalar(@dirlist)-1 ) {
rmdir($dirlist[$a]);$a-=1;
}
}
Just a side note: When using these make sure you run -d tests if you are not certain your target is a folder or a file. If it's just a file you can } else { unlink(file);}
sub fmove {
my ($i,$data,$mvo,$mvn);
open($mvo,"<",$_[0]) or die $!; binmode($mvo);
open($mvn,">",$_[1]) or die $!; binmode($mvn);
while (($i=sysread $mvo,$data,4096)!=0){print $mvn $data};
close($mvn);close($mvo);unlink($_[0]);
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
… having people tell me to use modules. … a required external module seems pretty lame.
I think these words were what itched afoken (and possibly other monks, too). A great deal of Perl's power comes from its already available modules.
The "extern"-ness of File::Copy and File::Path is such that it is just a use statement away - no installation needed.
Nothing against writing this anew from scratch -- as a learning experience, to explore new ways, or for special needs. But the possibility to use modules (and to create them, for your own custom logic that you need in multiple places) is one of Perl's strengths.
| [reply] [Watch: Dir/Any] |
|
I understand completely. I see now that my statement could have been interpreted a little harsher than I ever intended. It wasn't really a bash on solid modules at all. It was more of a personal complaint that the language didn't have built in functions as simple as deleting/copying/moving a folder recursively. I do thank you for your input. I also updated the dir_del. Found a bug in it lol. I am working on a small package of subs that will allow all file/folder copy/move/delete from all perl built-ins. Once again, mostly for fun and to see it be done. Nothing against modules as a whole. I think they're great and I have had the pleasure of using quite a few thus far. I've also had the displeasure of dealing with broken ones that don't build and having to make native workarounds like this...I sort of think it's fun. I never intended to offend anyone and yes I am new on here and "relatively" newish to perl I would say. Very strong Bash,BASIC,HTML,*nix administration/networking background so picking up on the basics of Perl was pretty easy for me but I admit I am still learning some of the do's and do not's. I appreciate honest critics as to learn properly. Even if I do throw something out there prematurely or just plain awful when viewed from a veteran :P
| [reply] [Watch: Dir/Any] |
|
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
|