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

Hello Monks,

I am wanting to copy a directory and all of its contents, including files and subdirectories, to another location. In addition, there are certain file extensions that may exist that I do not want copied, such as .lck which the script needs to account for. Unfortunatly, my search endevours have produced no hits.

I am running this on windows. I have explored XCOPY but Xcopy does not have a way to exclude or match a regular expression. I also was using File::Copy and File::Path. This was working, until it came upon a subdirectory. In which case the sub directory and the contents in the subdirectory were not copied to the new location.

So in a nutshell, i need to do exactally what "xcopy /E /I /F /R <source> <destination>" would do except that I need it to look for files such as ".lck" files and not copy them over

Any help and sample script would greatly be appreciated on how to accomplish this task

Replies are listed 'Best First'.
Re: Copy files and subdirectories
by matze77 (Friar) on Jun 09, 2011 at 15:25 UTC
      I looked into that module as well, but if I read it correctly, it did state that the developer was working on the EXCLUDE portion of it. So, in the mean time I went with the following code. If anybody has any other recommendations, where it can be portable, i would appreciate the feedback.
      my $source = 'c:/win/data'; my $to_dir= 'c:/temp/DATA'; use File::Path; mkpath(${to_dir}) or die "mkdir '${to_dir}' failed: $!" if not -e $to_ +dir; open(FH,">${to_dir}/EXCLUDE.txt"); print FH ".lck\n"; close FH; my $xcopy = $ENV{'systemroot'} . '\\system32\\xcopy.exe'; my $exclude="${to_dir}/EXCLUDE.txt"; $exclude =~ s/\//\\/g; my $command = qq{("$xcopy" "$source" "$to_dir" /E /I /F /R /Y /EXCLUDE +:$exclude)}; print $command . "\n"; system($command); unlink $exclude;
Re: Copy files and subdirectories
by BrowserUk (Patriarch) on Jun 10, 2011 at 00:35 UTC
    but Xcopy does not have a way to exclude ... I need it to look for files such as ".lck" files and not copy them

    Hm. According to the documentation:

    /EXCLUDE:file1[+file2][+file3]... Specifies a list of files containing strings. Each stri +ng should be in a separate line in the files. When any of +the strings match any part of the absolute path of the file +to be copied, that file will be excluded from being copied. F +or example, specifying a string like \obj\ or >>>.obj<<< wi +ll exclude all files underneath the directory obj or >>>all files w +ith the .obj extension<<< respectively.

    suggests that you've misread the documentation and that:

    "xcopy /E /I /F /R /exclude:.lck <source> <destination>"

    should do exactly what you are asking for.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Copy files and subdirectories
by eye (Chaplain) on Jun 10, 2011 at 00:23 UTC
    I believe there are versions of rsync for Windows; it would likely do what you want (it can exclude files, but I'm not familiar with xcopy or its options).