Home > Archive > PERL Beginners > April 2008 > copy ffiles and directories recursively
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
copy ffiles and directories recursively
|
|
| Alok Nath 2008-04-01, 4:04 am |
| Hi,
I wanted to recursively copy the contents of a directory(both directory and files) from one folder to the other.
The below code does only copy of directories but leaves behind files.
Can anybody help me how to do that ?
Regards,
Alok
#!/usr/bin/perl
$startDir = q{c:\test};
$newDir = q{c:\new};
&myReadDir($startDir);
exit 0;
sub myReadDir
{
my ($dir) = @_;
my (@dirs,$list);
opendir(DIR,$dir) || warn "can't open the directory $dir: $!\n";
@dirs=grep {!(/^\./) && -d "$dir\\$_"} readdir(DIR);
closedir (DIR);
for $list(0..$#dirs)
{
mkdir ($newDir) unless -d $newDir;
$ndir = $dir;
$ndir =~ s/c:\\test/c:\\new/;
print $ndir."\\".$dirs[$list],"\n";
mkdir ($ndir."\\".$dirs[$list]);
&myReadDir($dir."\\".$dirs[$list]);
}
return 1;
}
---------------------------------
No Cost - Get a month of Blockbuster Total Access now. Sweet deal for Yahoo! users and friends.
| |
| Jeff Pang 2008-04-01, 4:04 am |
| On 4/1/08, alok nath <aloknathlight@yahoo.com> wrote:
> Hi,
> I wanted to recursively copy the contents of a directory(both directory and files) from one folder to the other.
use File::Copy from CPAN.
> The below code does only copy of directories but leaves behind files.
> @dirs=grep {!(/^\./) && -d "$dir\\$_"} readdir(DIR);
because you picked up only the directories into the @dirs.
| |
| John W. Krahn 2008-04-01, 8:02 am |
| alok nath wrote:
> Hi,
Hello,
> I wanted to recursively copy the contents of a directory(both directory
> and files) from one folder to the other.
> The below code does only copy of directories but leaves behind files.
> Can anybody help me how to do that ?
http://search.cpan.org/~dmuey/File-...35/Recursive.pm
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
| Alok Nath 2008-04-01, 8:02 am |
| Hi,
There is a small problem now :
It now copies the files as directories :
#!/usr/bin/perl
$startDir = q{c:\test};
$newDir = q{c:\new};
&myReadDir($startDir);
exit 0;
sub myReadDir
{
my ($dir) = @_;
print "\n\nProcessing $dir \n" ;
my (@dirs,$list);
opendir(DIR,$dir) || warn "can't open the directory $dir: $!\n";
#@dirs=grep {!(/^\./) && -d "$dir\\$_"} readdir(DIR);
@dirs=grep {!(/^\./)} readdir(DIR);
closedir (DIR);
for $list(0..$#dirs)
{
print "list = $list\n" ;
mkdir ($newDir) unless -d $newDir;
#mkdir ($newDir) ;
$ndir = $dir;
$ndir =~ s/c:\\test/c:\\new/;
print $ndir."\\".$dirs[$list],"\n";
mkdir ($ndir."\\".$dirs[$list]);
&myReadDir($dir."\\".$dirs[$list]);
}
return 1;
}
Can somebody point how to fix this ?
Regards,
Alok.
"John W. Krahn" <krahnj@telus.net> wrote:
alok nath wrote:
> Hi,
Hello,
> I wanted to recursively copy the contents of a directory(both directory
> and files) from one folder to the other.
> The below code does only copy of directories but leaves behind files.
> Can anybody help me how to do that ?
http://search.cpan.org/~dmuey/File-...35/Recursive.pm
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
---------------------------------
You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.
| |
| Jim Gibson 2008-04-01, 7:05 pm |
| In article <674307.66163.qm@web53206.mail.re2.yahoo.com>, Alok Nath
<aloknathlight@yahoo.com> wrote:
> Hi,
> There is a small problem now :
>
> It now copies the files as directories :
>
> #!/usr/bin/perl
use strict;
use warnings;
>
> $startDir = q{c:\test};
> $newDir = q{c:\new};
> &myReadDir($startDir);
You don't need to put an ampersand when calling a subroutine.
> exit 0;
>
> sub myReadDir
> {
> my ($dir) = @_;
> print "\n\nProcessing $dir \n" ;
> my (@dirs,$list);
> opendir(DIR,$dir) || warn "can't open the directory $dir: $!\n";
> #@dirs=grep {!(/^\./) && -d "$dir\\$_"} readdir(DIR);
> @dirs=grep {!(/^\./)} readdir(DIR);
my @files = grep{!(/^\./)} readdir(DIR);
would be a better name than @dirs.
> closedir (DIR);
> for $list(0..$#dirs)
for my $file ( @dirs ) {
> {
Prepend the directory name to the file name and test if the result is a
directory:
if( -d "$dir/$file" ) {
# treat file as directory (make the directory)
}else{
# treat file as file (copy)
}
> print "list = $list\n" ;
> mkdir ($newDir) unless -d $newDir;
> #mkdir ($newDir) ;
> $ndir = $dir;
> $ndir =~ s/c:\\test/c:\\new/;
> print $ndir."\\".$dirs[$list],"\n";
> mkdir ($ndir."\\".$dirs[$list]);
> &myReadDir($dir."\\".$dirs[$list]);
> }
> return 1;
> }
> Can somebody point how to fix this ?
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
|
|
|
|
|