On Wed, Jul 25, 2001 at 04:22:01PM +0200, Birgit Kellner wrote:
> Hi,
>
> I am using File::Find and File::Copy to accomplish the following:
> check a directory tree for all files that end in *.html and copy them to a
> different directory, preserving the tree structure.
> The following code works fine provided that all subdirectories already
> exist. But is there a way to add the requirement "if the subdirectory does
> not yet exist, create it?"
Yes, File::Path::mkpath. For example:
use File::Basename qw(dirname);
use File::Path qw(mkpath);
> foreach $file (@files) {
>
> ### $destfile will be the copied file in subdir admin, $file the original
> file in $origdir or one of its subdirectories
>
> $file =~ m/($origdir)(.*\/)(.*)$/;
> $file =~ /($origdir)(.*?)(^\S+\.html)/;
> $destfile = "$targetdir$2$3"; #
mkpath(dirname($destfile));
mkpath will raise a fatal exception in the face of failure, so you may want
to wrap it in an eval block and check the return value.
> copy ("$file", "$destfile") || die (print "can't copy");
Your die here doesn't make much sense. The die function prints out its
arguments, you don't need to print the error message with print.
> push (@destfiles, $destfile);
>
> }
Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com
--
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]