in reply to copy files to another directory

A few things to look at:

  1. You don't appear to really be using File::Find. You should already have the files before that using readdir.
  2. When you grep for the *.rtf files, you don't want to join the resulting list. That will flatten it into a delimited scalar.
  3. See the File::Copy docs. The copy method takes two arguments: a source file and a target file. If you follow the suggestions above, you will have a list of files in @select_files. You want to iterate over that and copy each one, something like this:
    foreach my $file (@select_files) { copy($file, "C:\\temp\\$file") or die "Failed to copy $file: $!\n"; }

I'd put some debug in along the way to see what you have as you try some of these out.

You can use File::Find something like this:

use strict; use File::Find; use File::Copy; my $dir = "/tmp"; sub process_file { if ($File::Find::dir ne $dir) { $File::Find::prune = 1; return 0; } return 0 if ($_ !~ /\.rtf$/); copy($File::Find::name, "/tmp/foo/$_") or die "Failed to copy $_: +$!\n"; return 1; } find(\&process_file, $dir);

Because File::Find does a full directory tree traversal by default (goes into subdirectories) I don't find it as intuitive as just reading the directory with readdir when only looking in one directory. Maybe I'm missing something in the docs that another monk can fill me in on that limits its depth. I usually use the $Find::File::prune = 1 setting as shown above, but I've never been convinced that's the easiest or best way.

Replies are listed 'Best First'.
Re: Re: copy files to another directory
by Anonymous Monk on Feb 19, 2003 at 15:08 UTC
    Thanks for your help. Although I've never seen "$Find::File::prune = 1" I understand its use in the code. Thanks again....