On 8/9/07, Mr. Shawn H. Corey <[EMAIL PROTECTED]> wrote:
> Mr. Shawn H. Corey wrote:
> > Evyn wrote:
> >> ... how would
> >> I keep the structure?
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > use File::Basename;
> > use File::Copy;
> > use File::Find;
> > use File::Path;
> >
> > my $SrcDir = '.'; # Set to the source directory
> > my $DstDir = 'c:\\Documents\\AudioDownloadsWaiting\\';
> >
> > sub wanted {
> >
> > if( m{ \.gar \z }msx ){
> > ( my $dir = dirname( $File::Find::name ) ) =~ s{ \A $SrcDir
> > }{$DstDir}msx;
> > mktree({$dir]);
>
> # Oops, this should be:
> mktree([$dir]);
>
> > move( $File::Find::name, $dir );
> > }
> > }
> >
> > find( \&wanted, $SrcDir );
> >
> > __END__
> >
> >
>
> --
> Just my 0.00000002 million dollars worth,
> Shawn
>
> "For the things we have to learn before we can do them, we learn by doing
> them."
> Aristotle
There are a few problems with the code above
1. it is mkpath not mktree (although oddly enough it is rmtree)
2. File::Find::find does a chdir before calling wanted(), so you
cannot use relative path names for dest.
3. you are using a naked string in the regex part of a substitution
without \Q\E (what if the source directory is named is "foo[a-z]bar").
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use File::Copy;
use File::Find;
use File::Path;
use File::Spec;
my $SrcDir = './test[a-z]'; # Set to the source directory
my $DstDir = './test2';
for my $dir ($SrcDir, $DstDir) {
next if File::Spec->file_name_is_absolute($dir);
$dir = File::Spec->rel2abs($dir);
}
sub wanted {
if (m{ \.gar \z }msx) {
(my $dir = $File::Find::dir) =~ s{\A\Q$SrcDir\E}{$DstDir};
mkpath([$dir]);
move ($File::Find::name, $dir);
}
}
find( \&wanted, $SrcDir );
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/