Ok, here is a version with a little "magic" done.
#!/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 = "C:/Perl/eg"; #define full path to source folder my $newpath = "c:/Delme"; #define path to new folder $| = 1; # force auto flush of output buffer chdir($path) || die "cannot move to $path"; find (\&doCopy, $path); #Recursively search $path for files to copy sub doCopy { return if ! -f $File::Find::name; my $subTree = substr $File::Find::name, 1 + length $path; my $destFile = "$newpath/$subTree"; print "\$destFile = $destFile\n"; #copy ($_, $destFile) or print "File $_ cannot be copied. $!\n"; }
Prints:
$destFile = c:/Delme/example.pl $destFile = c:/Delme/Readme.txt $destFile = c:/Delme/aspSamples/ado1.asp $destFile = c:/Delme/aspSamples/ado10.asp $destFile = c:/Delme/aspSamples/ado11.asp $destFile = c:/Delme/aspSamples/ado12.asp $destFile = c:/Delme/aspSamples/ado13.asp $destFile = c:/Delme/aspSamples/ado14.asp $destFile = c:/Delme/aspSamples/ado15.asp ...
Note that you needn't interpolate variables into strings to pass them as parameters, just pass the variable: copy ($_, $destFile) for example.
In reply to Re^3: Copying files between directories. How can I improve this code.
by GrandFather
in thread Copying files between directories. How can I improve this code.
by richill
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |