in reply to Copying files from a directory to cwd

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: $!"; }

Replies are listed 'Best First'.
Re^2: Copying files from a directory to cwd
by Anonymous Monk on Apr 21, 2011 at 04:21 UTC

    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

        I changed the script a little bit as below,am geting the following error,the change is am passing the directory location to search as an argument,the location can be a server,how to find these files if it is on some server location?

        #!/usr/bin/perl -w use strict; use warnings; use Net::Telnet; use Net::FTP; use File::Copy; use File::Find; my $modem_build_location; $dir=$ARGV[0]; opendir(DIR, "$dir") || die "Error in opening dir $dir $!"; print "\n$modem_build_ms\n"; my %files = map {$_ => 1} qw(data.txt datascript.pl); find(sub { copy($File::Find::name, '.') or die "Can't cp $_: $!" if delete $files{$_}; }, $dir); close DIR; ERROR:- Can't cp datascript.pl: at Modem_images.pl line 19.

        This is not working,I am getting error Can't cp datafiles.txt: at Modem_images.pl line 19