I can't be sure, but I think this is what happened:

When your "update_file_dir" subroutine executes the "rename" function, this does not change the data values that the File::Find methods are giving you, so the original file names are pushed onto the @files array, not the modified file names. If the push had been like this:

-f $_ && push( @files, $concat );
things would have worked better the first time.

Then, in the following "foreach $file (@files)" loop, there is no error checking on "open(IN,$file)" -- if it had been:

unless (open(IN,$file)) { warn "Unable to open $file for modification\n"; next; }
you would have seen the problem with the push, that the old file names were being used instead of the new ones. (But as it was, the open failed quietly, as did the following attempt to read @lines -- no content was ever present in this array on the first run.)

There is a good chance that the open(OUT,">$file") call also failed quietly (there was no error checking there either), because by this time, all the directory names have changed, as well as the file names. This probably saved you from "a fate worse than death" (that's a very apt phrase for this situation).

If opening the old file names for output had succeeded, this would have created a set of empty files with the old file names. Then, when you ran the script a second time, those empty files would have been renamed to the new names, thus obliterating all the original file data. (You do have backups for all those files, don't you?)

Given that this catastrophe was avoided, the second run found all the modified file names, also found that there were no files with the old host in their file names (no renaming required), pushed all the new-host file names onto @files, and the "foreach $file" loop finally worked as intended.

Count your blessings, and thank your guardian angel...

update: Oh, I forgot to mention: when you are engaged in this sort of file manipulation, you really, really should include a lot of error checking, to make sure that things are going the way you intend them to go.


In reply to Re: Renames files and directories but won't update files at the same time. by graff
in thread Renames files and directories but won't update files at the same time. by kirk123

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.