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

Please forgive me, as i don't get much time for enjoying the features of perl but I'm having trouble understanding why a copy statement isn't actually copying anything? the basic precept of what you are seeing is the following, create a new directory (yes unfortunately on Windows), read a directory and copy everything to the new directory. I'm probably missing something basic, but my feable mind isn't following this one.
here is the code...
use strict; use File::Copy; use diagnostics -verbose; my $trailer = qw( \ ); my $dev_dir = 'V:\dblomber\authdev' . $trailer; my $qa_dir = 'V:\dblomber\authqa' . $trailer; my $qa_pre_new_dir = 'V:\dblomber\auth'; my $all_qa_files; #get date yyyymmdd to append to new dir my($sec,$min,$hour,$mday,$mon,$year) = localtime; my $current_date = sprintf("%04d%02d%02d", $year+1900,$mon+1,$mday); my $qa_new_dir = $qa_pre_new_dir . $current_date . $trailer; mkdir $qa_new_dir; print "CREATED NEW ARCHIVE DIR:$qa_new_dir:\n"; opendir (QA, $qa_dir) or die "Cannot open $qa_dir $!"; print "OPENED $qa_dir\n"; opendir (QAN, $qa_new_dir) or die "Cannot open $qa_new_dir $!"; print "OPENED $qa_new_dir\n"; foreach $all_qa_files (sort readdir(QA)) { enable diagnostics; copy ("$qa_dir$all_qa_files", "$qa_new_dir$all_qa_files") or d +ie "copy Failed: $!"; print "COPYING FILE:$qa_dir$all_qa_files\t to $qa_new_dir$all_ +qa_files\n"; disable diagnostics; }
here is the output.
CREATED NEW ARCHIVE DIR:V:\dblomber\auth20060221\: OPENED V:\dblomber\authqa\ OPENED V:\dblomber\auth20060221\ Uncaught exception from user code: copy Failed: at T:\My Documents\perl\release.pl line 37. at T:\My Documents\perl\release.pl line 37
Here is the question.
why doesn't this work?
if I comment out the "copy" statement the print statement appears correct but the copy is failing?
output of just print statement.
CREATED NEW ARCHIVE DIR:V:\dblomber\auth20060221\: OPENED V:\dblomber\authqa\ OPENED V:\dblomber\auth20060221\ COPYING FILE:V:\dblomber\authqa\. to V:\dblomber\auth20060221\. COPYING FILE:V:\dblomber\authqa\.. to V:\dblomber\auth20060221\. +. COPYING FILE:V:\dblomber\authqa\crap.txt to V:\dblomber\auth20 +060221\crap.txt COPYING FILE:V:\dblomber\authqa\sample.doc to V:\dblomber\auth20 +060221\sample.doc FINISHED COPYING FILES CLOSED V:\dblomber\authqa\

Replies are listed 'Best First'.
Re: Yet Another File::Copy problem
by GrandFather (Saint) on Feb 22, 2006 at 01:15 UTC

    Copying . to . (current folder) and .. to .. (parent folder) is probably not a good thing to do. I'd skip those with something like:

    next if $all_qa_files =~ /\.\.?$/;

    DWIM is Perl's answer to Gödel

      That should be

      next if $all_qa_files =~ /^\.\.?$/;

      But even that's not good enough. copy cannot copy directories, so you need to skip all directories, not just the special ones. The following will do the trick:

      next if not -f "$qa_dir$all_qa_files";
      interesting enough, skipping '.' and '..' resolved this issue. I appreciate that comment and glad I added it before waiting for other results.
      now, any explanation for it failing when now exlcluding '.' or '..' for my simple mind?

        Simply stated, '.' refers to the current directory; '..' refers to the current directory's parent. Trying to copy those causes problems, and is usually not what you intend. However, readdir reads those two as 'files', so you have to skip them.

        If you're still getting errors, you may need to check that you're actually creating the directories you think you are. In particular, I am suspcious of the ending backslash, which I believe may be an error. So - omitting the copy, are your directories actually being created? Also, I'm thinking that you need to be using forward slashes, even though you're on Windows. I know I do well enough when I use $dir="level1/level2/etc"; (note the double quotes). You can use this form for specifying a file path:  $path="$dir/$file";, which works.