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

I'm having a problem doing a simple file copy using an array holding specific file names (023.jpg, 024.gif, etc.)and I've traced the problem down to:
foreach $line(@lines){ copy ("$GraphicsDirectory/$line", "$pparent/$path/images/$line"); }
For some reason the above code generates a file or directory not found error because of the $line command. I've checked and $GraphicsDirectory/023.jpg, as well as C:/graphics/023.jpg exist if I run a -e test, but $GraphicsDirectory/$line or C:/graphics/$line do not. I've checked the $line values too. I'd appreciate any ideas. Naomi

Replies are listed 'Best First'.
Re: File Copy with Array
by matija (Priest) on Apr 14, 2004 at 16:39 UTC
    I assume you are using File::Copy?

    You say you checked the existance of $GraphicsDirectory - are you sure the source is the problem? Could it be that the non-existant part is $pparent/$path/images/$line?

    Can you put a statement like

    print "<$GraphicsDirectory><$line><$pparent/$path/images/$line>\n";
    in your loop and see if it returns reasonable values?

    The < and > are there so you can tell if any of the values are empty and where each variable begins and ends.

      Thanks everyone for the help. It turned out that the array was picking up a line feed from the text input file and trying to copy that as well. Using the < >'s helped because I noticed that the line kept breaking. I used a chomp function, and that seemed to take care of it. Thanks again! Naomi
Re: File Copy with Array
by revdiablo (Prior) on Apr 14, 2004 at 17:26 UTC

    Taking the wild-speculation ball and running with it, if matija's and dragonchild's intuitions are right, your next question will be "ok, now how do I make the path if it doesn't exist?" As a pre-emptive strike, I offer up File::Path with its handy mkpath function. This will recursively create any parent paths necessary to get the one you want.

Re: File Copy with Array
by dragonchild (Archbishop) on Apr 14, 2004 at 17:04 UTC
    To elaborate on matija's answer, copy() will not create $pparent/$path/images/$line if $pparent/$path doesn't already exist. Some versions of the cp on Unix will do so, if passed a specific flag (-p, for example), but most will not. It's up to you to make sure that the directory you're copying files into exists before the copy is attempted. Using -d may help.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

use File::Flat;
by PodMaster (Abbot) on Apr 15, 2004 at 07:24 UTC
    File::Flat - Implements a flat filesystem meaning you just File::Flat->copy( "$GraphicsDirectory/$line", "$pparent/$path/images/$line") and "$pparent/$path/images/$line" will magically spring into existance even if $pparent or $pparent/$path or $pparent/$path/images doesn't exist. Be sure to note that File::Flat uses File::NCopy and File::Remove which may have portability issues.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: File Copy with Array
by blue_cowdawg (Monsignor) on Apr 14, 2004 at 18:42 UTC

        I'd appreciate any ideas. Naomi

    To amplify what has already been said:

    use File::Path; use File::Copy; : : {mumble..mumble} much handwaving foreach my $line(@lines){ # flip the 0 to a 1 if you want verbose output # see text below for what 0755 means. mkpath([ "$pparent/$path/images" ],0,0755) if not -d "$pparent/$path/images"; copy ("$GraphicsDirectory/$line", "$pparent/$path/images/$line"); }

    The 0755 in the mkpath can be changed to reflect the octal permissions you want the directory to have. If the directory does not exist that code will create the directory and the path leading to it if necessary. See the man pages for File::Path if you need more detail.

    Two lines of code can make all the difference in the world.

Re: File Copy with Array
by periapt (Hermit) on Apr 14, 2004 at 18:36 UTC


    Another option to consider is directory permissions. Your use of C:/graphics/$line suggests a win32 machine. I've received this same message during copy when I did not have access permissions on an intermediate directory in the path. Can't say if this is an issue unique to windows directory permissions or a consequence of directory permissions in general. Actually , it has only happened once but I spent three days chasing it down. I guess it stuck with me.

    PJ