in reply to Copying files between directories. How can I improve this code.
First off, uncomment use strict;! Then add the my to declare each variable. Note though that you need to declare @Filenames outside the loop.
Note that #recursively search $path for files is a lie. There is no recursion and the search will only find files in the top level directory. If you want to actually perform a recursive search you need to do a little more work. At that point File::Find is likely what you want. Taking that route, something like the following (untested) code is what you want:
#!/usr/local/bin/perl -W # force taint checks, and print warnings use strict; # install all three strictures use File::Copy; use File::Find; my $path = "sourceDirectory"; #define full path to source folder my $newpath = "destinationDirectory"; #define path to new folder my @Filenames; $|++; # force auto flush of output buffer find (\&doCopy, $path); #Recursively search $path for files to copy sub doCopy { return if ! -f $File::Find::name; chdir($path) || die "cannot move to $path"; my $destFile = $newpath . $_; print "\$destFile = $destFile\n"; copy( "$_" , "$destFile" ) or print "File $_ cannot be copied. $!\ +n"; }
Note that you will probably have to clean up issues regarding paths to destination sub-directories in copy. You will probably need to do some magic with $File::Find::dir to strip off the root path and append the remaining path to $newpath.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Copying files between directories. How can I improve this code.
by richill (Monk) on Apr 22, 2006 at 01:09 UTC | |
by GrandFather (Saint) on Apr 22, 2006 at 04:14 UTC | |
by richill (Monk) on Apr 22, 2006 at 08:48 UTC | |
by GrandFather (Saint) on Apr 22, 2006 at 08:54 UTC |