Recursively copying a directory

L

Lénaïc Huard

-----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.

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
//\ (e-mail address removed)
V_/_ KeyID: 0x04D2E818
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBKFnIjYEjJATS6BgRAsaCAKCHuCyqllVr59P1cnxID9NyONEDJACgqOVn
hCEkKvRqSQwQ+hTiTI/14bQ=
=85pH
-----END PGP SIGNATURE-----
 
S

Sherm Pendley

Lénaïc Huard said:
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.

#!/usr/bin/perl

use strict;
use warnings;

use File::NCopy;

my $copier = new File::NCopy('recursive'=>1);
$copier->copy('dir1', 'dir2');

sherm--
 
G

Gregory Toomey

Lénaïc Huard said:
-----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
 
J

Jürgen Exner

Gregory said:
Why reinvent the wheel? I use system all the time
system("cp -R dir1 dir2");

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
 
T

Tore Aursand

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)?
 
G

gumby

Lénaïc Huard said:
-----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.

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
//\ (e-mail address removed)
V_/_ KeyID: 0x04D2E818
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBKFnIjYEjJATS6BgRAsaCAKCHuCyqllVr59P1cnxID9NyONEDJACgqOVn
hCEkKvRqSQwQ+hTiTI/14bQ=
=85pH
-----END PGP SIGNATURE-----


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);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top