If you want make a list of all the files with names ending in '.rtf' in a particular directory (name already in $dir):use File::Find; use File::Copy; sub find_copy_rename { # THIS FUNCTION IS NOT THE ONE CALLED BELOW # WHY IS IT HERE? my @dir = $fixed_path; # HERE YOU ARE ASSIGNING A SCALAR TO AN ARRAY # It will work, but why are you doing it? my @get_directory = substr($fixed_path,-12); # HERE YOU ARE ASSIGNING A SCALAR TO AN ARRAY # It will work, but why are you doing it? # Why are you using the last 12 characters of $fixed_path? # How do you know that is what you want? my $get_each_directory = @get_directory; # HERE YOU ARE ASSIGNING AN ARRAY TO A SCALAR # AFTER THIS '$get_each_directory' WILL EQUAL THE NUMBER 1 my $dir = @dir; # HERE YOU ARE ASSIGNING AN ARRAY TO A SCALAR # AFTER THIS '$dir' WILL EQUAL THE NUMBER 1 # PROBABLY THIS IS NOT WHAT YOU WANT my $current_path = 'C:\\temp\\'; # HERE $dir == 1, so this will always FAIL if ($File::Find::dir ne $dir) { $File::Find::prune = 1; return 0; } # IT LOOKS LIKE YOU WANT TO ONLY LOOK IN THE DIRECTORY # ASSOCIATED WITH $fixed_path. IF SO, CONSIDER USING # readdir() or glob() RATHER THAN File::Find return 0 if ($_ !~ /\.rtf$/); copy($File::Find::name, $current_path.$_) or die "Failed to copy $ +_: $!\n"; return 1; } find(\&process_files, $dir); # WHERE IS THIS FUNCTION 'process_files'? my @process_file = &process_files; # NOW YOU ARE CALLING 'process_files' AGAIN. # ALMOST CERTAINLY WRONG # Functions used in File::Find do not return a list of files # You can append to a list in the function called from # File::Find my $get_files = grep { !-d } @process_file; # NO, THIS IS WRONG # HERE 'get_files' IS SET TO THE _number_ OF NON-DIRECTORY # FILES IN THE ARRAY @process_file foreach my $get_files (@process_file) { my $newfile = $get_files; $newfile =~ s/\$mrn.$get_each_directory.'.'.rtf$/word1.rtf/; # THE VARIABLE '$get_each_directory' IS NOT IN SCOPE HERE # (AT LEAST IN THE CODE WE SEE) # THE LEFT-HAND SIDE OF YOUR SUBSTITUTION MAKES NO SENSE # IT IS ALREADY QUOTED SO YOUR USE OF ' and . IS ERRONEOUS if (-e $newfile) { # YOU (MAY) NEED TO INCLUDE A DIRECTORY HERE # UNLESS $newfile INCLUDES A DIRECTORY OR YOU # ARE LOOKING FOR IT IN THE CURRENT DIRECTORY warn "can't rename $get_files to $newfile: $newfile exists\n"; } elsif (rename "$newdir/$get_files", "$newdir/$newfile") { # WHAT IS '$newdir' ???? print "file was renamed to $newfile\n" } else { warn "rename $get_files to $newfile failed: $!\n"; } }
Note that the elements of this array include the directory name. If you want to search subdirectories, too, then you should use File::Find:my @rtf_in_dir = glob("$dir/*.rtf");
use File::Find; my @rtf_in_subddirs; sub wanted { if (/\.rtf$/) { push @rtf_in_subdirs, $File::Find::name; } } find(\&wanted, $dir);
In reply to Re: Find, copy & rename
by Thelonius
in thread Find, copy & rename
by skyler
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |