Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

attempt to copy files and directories to new location

by grashoper (Monk)
on Apr 15, 2009 at 18:11 UTC ( [id://757753]=perlquestion: print w/replies, xml ) Need Help??

grashoper has asked for the wisdom of the Perl Monks concerning the following question:

update..I changed to use file:copy:recursive.. here is the code which works just fine.
use File::Copy::Recursive; use File::Find; use strict; my $srcdir='c:\\inetpub\\performancetesting\\output\\new\\'; my $destdir='c:\\'; File::Copy::Recursive::dircopy $srcdir, $destdir or die "Copy failed: +$!";
as part of a project I am working on I need to copy part of a directory tree to the root, this is to move files being processed by something else so that I can load them into a db. unfortunately its not working out yet, all i get is my warning can't open the file yet for each folder what stupid mistake am I making this time
use File::Copy; my $dir="c:\\inetpub\\performancetesting\\output\\new\\mlx\\"; my $dir2="c:\\"; lista_dirs($dir,$dir); sub lista_dirs{ my ($dir,$dirname)=@_; my (@dirs,@files); opendir DIR, "$dir"; my @dircontent=grep {/[^\.]/} readdir(DIR); closedir DIR; @dirs=(); @files=(); foreach(@dircontent){ if (-d "$dir/".$_){ push @dirs,$_; copy($dir,$dir2); } else { push @files,$_ if($_=~/\.txt$/); } } foreach my $d(@dirs){ lista_dirs("$dir/".$d,$d); print $d,"\n" ; #print $dir; prints full path? } }# not sure if this is going to work or not. opendir(IN,"$dir") or die "opening directory failed:$!"; while (defined (my $file = readdir(IN)) ) { push (my @files, $file); foreach my $filename (@files) { copy ("$dir".$filename, "$dir2".$filename) or warn "Can't open the fil +e yet $filename\n:$!"; } } closedir(IN);

Replies are listed 'Best First'.
Re: attempt to copy files and directories to new location
by ikegami (Patriarch) on Apr 15, 2009 at 18:23 UTC
    Why do you call copy repeatedly with the same arguments? Check its result for errors while you're at it.
Re: attempt to copy files and directories to new location
by CSJewell (Beadle) on Apr 15, 2009 at 18:29 UTC

    You may wish to start with correcting the first call to lista_files. It's currently lista_dirs($dir,$dir); and I assume it's supposed to be lista_dirs($dir,$dir2);

    Then add use strict; and use warnings; before the use File::Copy;

    Then we'll see what else needs to be corrected.

      I assume it's supposed to be lista_dirs($dir,$dir2);

      Probably, but then again, the second parameter is never used.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://757753]
Approved by kennethk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-20 11:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found