tags:

views:

44

answers:

3

Right now, I am using something like

copy catfile($PATH1, "instructions.txt"), catfile($ENV{'DIRWORK'});

individually for each of the .txt files that I want to copy. This code is portable as it does not use any OS specific commands.

How can I copy all the text files from $PATH1 to DIRWORK, when I do not know the individual names of all the files, while keeping the code portable?

A: 

If you are guaranteed to work on a Unix system (e.g. don't care about portability), I'll go against my own usual inclinations and accepted best practices and recommend considering using "cp" :)

system("cp $PATH1/*.txt $ENV{'DIRWORK'}"); 
# Add error checking and STDERR redirection!

For Perl native solution, combine globbed file list (or File::Find) with File::Spec ability to find actual file name

my @files = glob("$PATH1/*.txt");
foreach my $file (@files) {
    my ($volume,$directories,$filename) = File::Spec->splitpath( $file );
    copy($file, File::Spec->catfile( $ENV{'DIRWORK'}, $filename ) || die "$!";
}
DVK
@DVK: cannot use `cp` for the reason that you mentioned - portability.
Lazer
+1  A: 

You can use the core File::Copy module:

use File::Copy;
my @files = glob("$PATH1/*.txt");

for my $file (@files) {
    copy("$PATH1/$file", $ENV{DIRWORK}) or die "Copy failed: $!";
}
eugene y
assumes a working dir of `$PATH1` to work
mfontani
@mfontani: good catch, updated
eugene y
`<$PATH1/*.txt>` also is a glob ;)
mfontani
+1  A: 

Using core File::Find and File::Copy and assuming you want all .txt files in $PATH1 copied to $ENV{DIRWORK}, and also assuming you want it to recurse...

use strict;
use warnings;
use File::Find;
use File::Copy;

die "ENV variable DIRWORK isn't set\n"
    unless defined $ENV{DIRWORK} and length $ENV{DIRWORK};
die "DIRWORK $ENV{DIRWORK} is not a directory\n"
    unless -d $ENV{DIRWORK};

my $PATH1 = q{/path/to/wherever};
die "PATH1 is not a directory" unless -d $PATH1;

find( sub{
    # $_ is just the filename, "test.txt"
    # $File::Find::name is the full "/path/to/the/file/test.txt".
    return if $_ !~ /\.txt$/i;
    my $dest = "$ENV{DIRWORK}/$_";
    copy( $File::Find::name, $dest ) or do {
        warn "Could not copy $File::Find::name, skipping\n";
        return;
    }
}, $PATH1 );

Give it a go ;)

Alternatively, why don't you use bash ?

$ ( find $PATH1 -type f -name '*.txt' | xargs -I{} cp {} $DIRWORK );
mfontani