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

Monks,

I am working on a small script to back up of some files that I am currently working on. The problem that I'm encountering is that when I run the script, I get a "Directory or file not found" error.

A short version of the code I'm using is:

use strict; use warnings; use diagnostics; use File::Copy; my $from = "path/to/directory/*.cmd"; my $to = "path/to/new/directory/*.cmd"; copy($from, $to) or die "Copy failed: $!";
The files that I want to transfer are in the directory pointed to in $from and I am able to successfully reach the location in $to. I have tried several variations of adding and removing *.cmd as well as adding a  / to the beginning of the path.

I'm sure that I'm missing some trivial detail, however I've been spending more time on this script that I really should and have been coming up empty.

Replies are listed 'Best First'.
Re: Directory or file not found while using copy
by jethro (Monsignor) on Sep 15, 2011 at 17:22 UTC

    Read the documentation: File::Copy. It clearly says that the first parameter to the copy() function is a file, singular! If you want to copy multiple files you might for example use the glob() function and copy the files in a loop or use system() to call the copy command provided by the shell

Re: Directory or file not found while using copy
by blue_cowdawg (Monsignor) on Sep 15, 2011 at 18:16 UTC

    Here's some fuel for thought:

    #!/usr/bin/perl -w use strict; use File::Copy; do_copy("path/to/dir","path/to/dest"); exit(0); sub do_copy { my ($src_path,$dest_path)=@_; opendir(DIR,$src_path) or die "$src_path: $!"; while(my $entry=readdir(DIR)){ next if $entry eq '..'; next if $entry eq '.'; my $sfqp = $src_path . "/" . $entry; my $dfqp = $dest_path . "/" . $entry; if ( -d $sfqp ) { mkdir $dfq,0755; # Change to your mask do_copy($sfqp,$dfqp); # Recursion } elsif ( -f $sfqp ) { copy $sfqp,$dfqp or die $! } } }

    Now, if you just want to move files ending in ".cmd" you'd add a line just before the copy call something like:

    next unless $entry =~ m@\.cmd$@;
    so it will ignore all but what you want.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Directory or file not found while using copy
by wwe (Friar) on Sep 16, 2011 at 08:11 UTC
    all of the previous posts point you to the right direction: File::Copy accepts only a single file as argument. But cpan also provides a File::Copy::Recursive module which allows to copy whole directories and also allows you to provide a glob'ed file mask using a rcopy_glob() method.

    Be aware about an ugly bug which strikes espetially if you want copy files from/to UNC path on windows.

Re: Directory or file not found while using copy
by pvaldes (Chaplain) on Sep 15, 2011 at 18:46 UTC
    Are you writing "path/to/directory/ when you want to write "/path/to/directory/?
Re: Directory or file not found while using copy
by pvaldes (Chaplain) on Sep 15, 2011 at 18:49 UTC
    Quick fix
    use strict; use warnings; use diagnostics; my $from = "path/to/directory/*.cmd"; my $to = "path/to/new/directory/"; `cp $from $to` or die "Copy failed: $!";
    Update: arrg, instead a reply now the former message is lost. This storm is doing strange things with the net...