RickNak has asked for the wisdom of the Perl Monks concerning the following question:

While I've worked with Perl for a number of years, I just never had need to use File::Copy. When I tried, however, I came up short trying to copy with an asterisk for a wildcard.

#!/usr/bin/perl -w # use diagnostics; use strict; use warnings; use File::Copy; # copy("*.txt", "DIR1"); copy("one.txt", "DIR2")

What am I doing wrong? DIR1 is empty, and DIR2 has the one file.

Rick

Replies are listed 'Best First'.
Re: File::Copy and wildcards
by stevieb (Canon) on Dec 11, 2019 at 05:13 UTC

    File::Find::Rule is a nice, easy to use distribution that allows you to use any valid regex (including standard glob type *.txt wildcards) to search with. Here's an example using it, along with File::Copy.

    use warnings; use strict; use File::Copy; use File::Find::Rule; my $from_dir = '/home/steve/repos/berrybrew'; my $to_dir = 'out'; my @files = File::Find::Rule->file() ->name('*.txt', '*.cs') ->in($from_dir); for (@files){ copy $_, $to_dir or die $!; }

    That copied all *.txt and *.cs files from one directory to another, recursively.

    To disable recursion, add another chained method call:

    ->in($from_dir) ->maxdepth(0); # descend zero levels below root of $from_dir
Re: File::Copy and wildcards
by swl (Prior) on Dec 11, 2019 at 04:13 UTC

    Does File::Copy support wild cards?

    There is nothing in the documentation to say it does, and a quick check of the source code indicates it does not use any of the glob functions.

Re: File::Copy and wildcards
by eyepopslikeamosquito (Archbishop) on Dec 11, 2019 at 10:46 UTC

    Dug out a primitive little helper function I wrote years ago in case it's of use. Just does a simple non-recursive copy of wild card files from one dir to another. Not proud of it but it still seems to work fine. Might be useful if you just need something small and simple:

    use strict; use warnings; use File::Copy; use File::Basename; =head2 copy_files_wild FROMDIR TODIR VERBOSE WILD Copy files in wildcard WILD from FROMDIR to TODIR. Return the number of files successfully copied. Die if something goes wrong. =cut sub copy_files_wild { my ( $fromdir, $todir, $verbose, $wild ) = @_; print "copy '$fromdir/$wild' to '$todir'.\n"; -d $fromdir or die "error: dir '$fromdir' not found\n"; -d $todir or die "error: dir '$todir' not found\n"; $fromdir =~ s/ /\\ /g; # Escape any spaces in $fromdir my $ncopy = 0; while ( glob("$fromdir/$wild") ) { next unless -f; my $fname = File::Basename::basename($_); my $targ = "$todir/$fname"; $verbose and print "$fname\n"; if ( File::Copy::copy( $_, $targ ) ) { ++$ncopy; } else { die "error: copy '$_' to '$targ':$!"; } } print "done.\n"; return $ncopy; } # Example my $ncopied = copy_files_wild( 'DIR1', 'DIR2', 1, '*.txt' ); print "$ncopied files copied\n";

      Yes, glob is one possible solution, however, there are quite a few caveats to its use, especially when interpolating variables into it and using it in scalar context: To glob or not to glob

Re: File::Copy and wildcards
by tybalt89 (Monsignor) on Dec 11, 2019 at 14:50 UTC

    untested

    copy($_, "DIR1") for glob "*.txt";
Re: File::Copy and wildcards ( Path::Tiny )
by Anonymous Monk on Dec 11, 2019 at 06:17 UTC

    See Path::Tiny it uses File::Copy

    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; for my $file ( path('.')->children(qr/.txt$/i) ) { $file->copy("DIR1/"); } path("one.txt")->copy("DIR2/");