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

I'm finally close to get it working. However I run this code but it doesnt copy any files. It doesn't give me any errors. Could anybody look at my code to see if I'm missing anything or need to change my code??? Basically the code reads a file an grabs an array of directory paths, then I use File::Find recursevily to go over each one of those path to copy a "word1.rtf" file to a new directory call "C:\testfiles" I really need help ...I've been working on this for two weeks without success. Now, I could change my code to copy only one file and it stops. But I want to be able to go to every single directory and grab its RTF file. I know that I'm close ..please help!!
#! perl use strict; use File::Copy; use File::Find; no warnings 'File::Find'; my $infile = "c:\\doclist1.chr"; my @directories; # first lets build our directory list open IN, "<$infile" or die "Couldn't open $infile, $!"; while (<IN>) { chomp; my @fields = split /,/; my $path_str = $fields[6]; do { warn "Empty field 7"; next } unless $path_str; my @path = split /\\/, $path_str; # push the directories into an array push @directories, join "\\", @path[ 0, 5, 6 ]; } close IN; exit; # now we have a directory list in @directories let's process it # for my $dir ( @directories ) { # process_directories($dir); #} sub process_directories { if (/\.rtf$/) { push @directories, $File::Find::name; copy($File::Find::name, "C:\\testfiles\\$_") or die "Failed to cop +y $_: $!\n"; } } find(\&process_directories, $File::Find::dir);

Replies are listed 'Best First'.
Re: finding files and copying them using File::Fiind & File::Copy
by steves (Curate) on Feb 25, 2003 at 07:06 UTC

    You seem to keep shuffling code around. How could the above even be close to working? You have an exit right after you get the directory names. If you take that out, the loop that used to process each one is now commented out. Instead, you're now calling find with the directory $File::Find::dir which is a package variable find initializes during a find ... so you're asking find to look in no directories now (I think) since that would be undefined until a find call is made.

    What's going to give you the most mileage here is to break down each piece and start small, adding debugging to make sure it's doing what you think. e.g., instead of jumping directly to trying to traverse directories and copy files, how about traversing them and printing the file names? That way you know what you're getting before you try to copy it. That way you can also show us some output so we can see what the program thinks it's doing.

    For me, I think it's time to:

    find(\&sleep, $bed);