mirswith
mirswith

Reputation: 1345

using perl's File::Xcopy to copy a directory ignoring files using regex

Perl's xcopy has the method fn_pat to specify a regular expression for the pattern matching and I want to use this to recursively copy a directory ignoring all files/folders that any of these strings:

.svn
build
test.blah

I am stumbling with the syntax to do that, I have looked over many perl regular expression guides but for the life of me I just can not get the hang of it. I appreciate any help.

Thanks.

... update ...

I found a perl regex that seems to be working, just not with xcopy's fn_pat. Not sure if this is a bug with xcopy or if my expression is not correct, however my tests show its ok.

$exp = '^(?!.*(\.svn|build|test\.blah)).*$';
if( '/dev/bite/me/.svn' =~ $exp ){ print "A\n"; }
if( '/dev/bite/me/.svn/crumbs' =~ $exp ){ print "B\n"; }
if( '/dev/build/blah.ext' =~ $exp ){ print "C\n"; }
if( '/dev/crap/test.blah/bites' =~ $exp ){ print "D\n"; }
if( '/dev/whats/up.h' =~ $exp ){ print "E\n"; }

only E prints as I was hoping. I'm curious to know if this is correct or not as well as to any ideas why its not working with xcopy.

Upvotes: 0

Views: 1015

Answers (1)

ErikR
ErikR

Reputation: 52049

Here is where File::Xcopy calls File::Find::finddepth:

sub find_files {
    my $self = shift;
    my $cls  = ref($self)||$self; 
    my ($dir, $re) = @_;
    my $ar = bless [], $cls; 
    my $sub = sub { 
        (/$re/)
        && (push @{$ar}, {file=>$_, pdir=>$File::Find::dir,
           path=>$File::Find::name});
    };
    finddepth($sub, $dir);
    return $ar; 
}

Here $re is your regexp.

According to the File::Find docs, $_ will be set to just the leaf name of the file being visited unless the no_chdir option used.

The only way I can see to get the no_chdir option passed to finddepth is to monkey-patch File::Xcopy::finddepth:

use File::Xcopy;

*{"File::Xcopy::finddepth"} = sub {
    my ($sub, $dir) = @_;
    File::Find::finddepth({ no_chdir => 1, wanted => $sub}, $dir);
};

Upvotes: 1

Related Questions