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

Dear Monks - I am trying to copy files "data.txt" and "datascript.pl" present in directory "C:\files" to a folder "scripts" in the current working directory.Can anyone pls advise how can I do that?

#!/usr/bin/perl -w use strict; use warnings; DIR=C:\files C:\files\data.txt C:\files\scripts\datascript.pl opendir(DIR, "$dir") || die "Error in opening dir $dir $!";

Replies are listed 'Best First'.
Re: Copying files from a directory to cwd
by wind (Priest) on Apr 21, 2011 at 04:16 UTC

    Just use File::Copy

    use File::Copy; use strict; use warnings; for (qw(C:\files\data.txt C:\files\scripts\datascript.pl)) { copy($_, '.') or die "Can't cp: $!"; }

      There is a small correction.All I know is that there are two files in data.txt and datascript.pl in "C:\files" directory .I dont know where these two files are exactly located inside "C:\files".How to find these two files inside C:\files and then copy?

        Use File::Find
        use File::Copy; use File::Find; use strict; use warnings; my %files = map {$_ => 1} qw(data.txt datascript.pl); find(sub { copy($File::Find::name, '.') or die "Can't cp $File::Find::name: $ +!" if delete $files{$_}; }, 'C:\\files');

        Update: change error message to display full path instead of just file name