We've drawn lots and I came up with the short stick. It's my duty to tell you that nearly every line of your program is wrong. (I'm reminded of what Mary McCarthy said about Lillian Hellman's writing.)
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"; } }
If you want make a list of all the files with names ending in '.rtf' in a particular directory (name already in $dir):
my @rtf_in_dir = glob("$dir/*.rtf");
Note that the elements of this array include the directory name. If you want to search subdirectories, too, then you should use File::Find:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.