Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recursively copying a directory

4 views
Skip to first unread message

Lénaïc Huard

unread,
Aug 22, 2004, 4:30:54 AM8/22/04
to
-----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
//\ Lenaic...@laposte.net
V_/_ KeyID: 0x04D2E818
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

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

Sherm Pendley

unread,
Aug 22, 2004, 4:56:10 AM8/22/04
to
Lénaďc Huard wrote:
> 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--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

Gregory Toomey

unread,
Aug 22, 2004, 8:54:27 PM8/22/04
to
Lénaïc Huard wrote:

> -----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ürgen Exner

unread,
Aug 23, 2004, 10:29:57 AM8/23/04
to
Gregory Toomey wrote:

> Lénaďc Huard wrote:
>> 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");

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


Tore Aursand

unread,
Aug 23, 2004, 10:44:25 AM8/23/04
to
On Mon, 23 Aug 2004 10:54:27 +1000, Gregory Toomey wrote:
>> 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");

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)

Lénaïc Huard

unread,
Aug 23, 2004, 4:49:27 PM8/23/04
to
>> the perl function equivalent to the "cp -R" Unix command.
>
> use File::NCopy;


Exactly what I was looking for.
Thanks !

gumby

unread,
Sep 13, 2004, 10:25:15 AM9/13/04
to
Lénaďc Huard <lenaic...@laposte.net> wrote in message news:<41285a53$0$29675$636a...@news.free.fr>...


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

0 new messages