Hello,
I'm looking for a way to copy a directory and all its content with perl ;
that is to say the perl function equivalent to the "cp -R" Unix command.
I've looked the 'File::Copy' package, but I only managed to copy regular
files and not directories.
If toto is a directory, the following lines create an empty regular file
named tata, whereas I expected tata to be new directory containing a copy
of toto's content.
use File::Copy;
copy( "toto", "tata" );
Does anybody have a nicer idea than a
system( "cp -R toto tata" );
which is not very portable...
Thanks,
Lénaďc.
- --
(o_ Lénaďc HUARD
//\ Lenaic...@laposte.net
V_/_ KeyID: 0x04D2E818
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFBKFnIjYEjJATS6BgRAsaCAKCHuCyqllVr59P1cnxID9NyONEDJACgqOVn
hCEkKvRqSQwQ+hTiTI/14bQ=
=85pH
-----END PGP SIGNATURE-----
#!/usr/bin/perl
use strict;
use warnings;
use File::NCopy;
my $copier = new File::NCopy('recursive'=>1);
$copier->copy('dir1', 'dir2');
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello,
>
> I'm looking for a way to copy a directory and all its content with perl ;
> that is to say the perl function equivalent to the "cp -R" Unix command.
Why reinvent the wheel? I use system all the time
system("cp -R dir1 dir2");
gtoomey
Reason 1: portability: the intended target system may very well not have a
'cp' program or has a 'cp' program that does something else or uses a
different parameter format or or or ...
Reason 2: efficiency: it is way more expensive to fork a new process and
exec a program than to use Perl's build-in functions. Granted, in this
particular case, where the job includes potentially copying thousands of
files on the HD, it's a mute point.
But in general it is a better idea to use Perl in Perl program.
jue
> Why reinvent the wheel? I use system all the time system("cp -R dir1
> dir2");
Because it's nice to have scripts which will work on any platform (or at
least as many platforms as possible)?
--
Tore Aursand <to...@aursand.no>
"First, God created idiots. That was just for practice. Then He created
school boards." (Mark Twain)
Exactly what I was looking for.
Thanks !
I needed to copy a directory with all the subs as well and tried to
import Ncopy and found that the company i work for doesnt have a
version of perl installed that has Ncopy. After asking they told me i
would have better luck winning the lottery than getting a version
installed on the diffrent sites we have. So i use these two subs that
could easily be combined into one if you wanted to (I know there is
room for improvement but it is something that works well for people
who cant get Ncopy).
sub copyFile
{
my ($copyFile, $path) = @_;
unless(copy($copyFile, $path))
{
failedToCopy($copyFile, $path);
}
}
sub failedToCopy
{
my ($copyFile, $path) = @_;
$copyFile =~ /\/(\w+)+$/;
my $dir = $path.$1.'/';
if(!-e $dir)
{
unless(mkdir($dir))
{
infoWindow($mw, "Error in CreateHdlProject, Failed to create
$dir. Please contact $programmer for support.", '650x200+0+0',
'red');
return;
}
}
my @subdirectoryFiles = <$copyFile/* >;
foreach(@subdirectoryFiles)
{
copyFile($_, $dir);
}
}