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

Hi:

I was able to use the code below to rename my files and directories recursively from XXXD25573786-2k2XX to XXX75219XX.

But I was unable to update the data in these files in the same manner.

I spend two days trying but can't get it right. How can I expand or modify the code below to update the files.

use File::Find; my $old_hostname= "D25573786-2k2"; my $new_hostname = "75219"; finddepth (\&update_file , "C:\/elmPaul" ); sub update_file_dir { if( /(.*)$old_hostname(.*)/i )# any files or directories { $orig = $File::Find::name ; $concat = $File::Find::dir . "\/$1$new_hostname$2"; rename($ori +g, $concat) || print "error can't rename $orig to $concat: $!"; } -f $_ && push (@files, "$File::Find::name"); } # end of sub update_file_dir foreach $file (@files ) { open (IN,"$file"); print "\nReading from file: $file \n"; @lines = <IN>; close IN; foreach (@lines) { if ( /(.*?)$old_hostname(.*?)/i ) { s/(.*?)$old_hostname(.*?)/$1$new_hostname$2/sgi; } open (OUT,">$file"); print OUT @lines; } }
If I ran the script the second time after the files and the directories had been renamed then it will update the files.

First time around the files and directories get rename and ONLY the file in "C:\elmPaul" get updated.

I also noticed that the script rename these files and directories from the bottom up. --kirk

  • Comment on Renames files and directories but won't update files at the same time.
  • Download Code

Replies are listed 'Best First'.
Re: Renames files and directories but won't update files at the same time.
by fokat (Deacon) on Aug 24, 2002 at 00:32 UTC
    I believe this code is much more complex than what it needs to be.

    What happens if you turn off the non-greediness in:

            if ( /(.*?)$old_hostname(.*?)/i ) 
              { 
                s/(.*?)$old_hostname(.*?)/$1$new_hostname$2/sgi;
              } 
    
    by saying something like

            if ( /(.*)$old_hostname(.*)/i ) 
              { 
                s/(.*)$old_hostname(.*)/$1$new_hostname$2/sgi;
              }  
     
    In this way it will match your prior regexp.

    Also, it would have been helpful if you wrote what the output is in the first run of your code.

    Also, according to those who know a lot about regexes, saying .* is actually bad as this regexp is very expensive. When you have the time, take a look around the fine Tutorials we have on the monastery. This helped me a lot with the regexes.

    Good luck.

      The construct

      if (/(CAPTURE BEFORE)REGEX(CAPTURE AFTER)/) { s/(CAPTURE BEFORE)REGEX(CAPTURE AFTER)/$1REPLACEMENT$2/; }

      is better written as a simple

      s/REGEX/REPLACEMENT/;

      — Arien

Re: Renames files and directories but won't update files at the same time.
by graff (Chancellor) on Aug 27, 2002 at 03:59 UTC
    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.

Re: Renames files and directories but won't update files at the same time.
by bart (Canon) on Aug 27, 2002 at 22:07 UTC
    First you rename the file and then you store the old path...

    Replace

    push (@files, "$File::Find::name");
    with
    push (@files, $concat);
Re: Renames files and directories but won't update files at the same time.
by thealienz1 (Pilgrim) on Aug 27, 2002 at 15:01 UTC

    You are calling \&update_dir for your find call, but the sub routine is sub update_file_dir. Maybe I am missing something, but I would change the call to update_file to update_file_dir.