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

Reposting a running code...I am getting the following error while running the following code,even if I change "\" to "/",I see the same error.Can anyone pls advise?

OUTPUT:- Can't cp C:\sensors/data.txt: at Test_script.pl line 19. #!/usr/bin/perl -w use strict; use warnings; use Net::Telnet; use Net::FTP; use File::Copy; use File::Find; my $dir; $dir=$ARGV[0]; opendir(DIR, "$dir") || die "Error in opening dir $dir $!"; print "\n$dir\n"; 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{$_}; }, $dir); close DIR;

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

    File::Find changes the cwd, so you'll need to cache that using Cwd in order to avoid trying to copy a file over itself

    #!/usr/bin/perl -w use strict; use warnings; use Cwd qw(getcwd); use File::Copy; use File::Find; my $dir = shift; print "$dir\n"; my $cwd = getcwd; my %files = map {$_ => 1} qw(data.txt datascript.pl); find(sub { copy($File::Find::name, $cwd) or die "Can't cp $File::Find::name: +$!" if (delete $files{$_}); }, $dir);
Re: Error while Copying files from a directory to cwd
by InfiniteSilence (Curate) on Apr 21, 2011 at 14:55 UTC

    When I take a gander at the source code for File::Copy I see that there are some OS-specific differences in the implementation. There's even a line of code that does this:

    return system('/bin/cp', '-f', $_[0], $_[1]) == 0;

    You didn't mention what OS you were running this on. File::Copy is written in Perl, however, so it should be easy enough to debug into it and find out exactly where it is crashing on you. Here is a simple Perl debugger tutorial you might use.

    Celebrate Intellectual Diversity

      I am running it on windows machine,it can be xp,200X OR win7

        What, you don't know what operating you are running?

        At any rate, you may or may not have read that tutorial I listed by now but for anybody else lurking around as Anonymous you might consider doing this to see for yourself what might be wrong:

        use strict; use warnings; use File::Copy; use File::Find; use Cwd qw|cwd|; my $modem_build_ms = 'I have no clue what should go into model_build_m +s'; my $modem_build_location; my $dir=$ARGV[0]; my $currdir = Cwd::cwd(); print $dir; opendir(DIR, "$dir") || die "Error in opening dir $dir $!"; #die "Bad directory" unless (-d $dir); print "\n$modem_build_ms\n"; my %files = map {$_ => 1} qw(data.txt datascript.pl); find(sub { copy($File::Find::name, $currdir) or die "Can't cp $File::Find::na +me : $! " . Cwd::cwd() if delete $files{$_}; }, $dir); close DIR; __END__ Notice how the output says this: I have no clue what should go into model_build_ms Can't cp ./other/data.txt : No such file or directory C:/Temp/abba/oth +er at (eva l 24) line 20.

        I ran this code like this C:\Temp\abba>perl  runall.pl ./other which means that somewhere somebody moved me down into the target folder. If you run this with the argument to the script as the FULL file path however c:/temp/abba/other the code runs as expected.

Re: Error while Copying files from a directory to cwd
by Anonymous Monk on Apr 21, 2011 at 15:53 UTC