Oh -- sorry, I must have misunderstood. Well, I wonder if there might be some problem about a file being "open" in some sense when you're trying to rename it, and MSWindows not letting you do that... though the code you showed makes this seem unlikely.

Still, it might be worthwhile to try a different different approach to the problem. Something like:

my $oldhostname = "whatever"; my $newhostname = shift || "somethingnew"; die "Nothing to do\n" if ( $oldhostname eq $newhostname ); print "changing $oldhostname to $newhostname\n"; open( FLIST, "<file_containing_list_of_filenames" ) or die "Unable to get list of file names to personalize\n"; my @oldfiles = <FLIST>; chomp @oldfiles; my @renamefiles = grep /$oldhostname/i, @oldfiles; s/$oldhostname/$newhostname/gi for @renamefiles; for my $file ( @oldfiles ) { my $newfile = ( $file =~ /$oldhostname/i ) ? shift @renamefiles : "$file.tmp"; open(IN, "<$file") or die "Unable to read $file: $!\n"; open(OUT, ">$newfile) or die "Can't write $newfile:$!\n; while (<IN>) { s/$oldhostname/$newhostname/gi; print OUT; } close OUT; close IN; unlink $file; rename $newfile, $file if ( $newfile eq "$file.tmp" ); }
Things to note: (1) if you're going to declare lexically scoped variables with "my", declare each variable as you need it, in the block where it will be used, not globally (which kind of defeats some of the value of lexical scoping).

(2) This approach uses a different strategy than your original -- since it knows it will need to rewrite every file anyway, it'll use a standard filtering approach, using "newhostname" to set the name of the output file where appropriate, then deleting the old file, and renaming the new one back to the original name if that wasn't supposed to change.

That's not tested, of course, but assuming that you have already taken care of creating the appropriate list of file names for input to the process, and have the host names the way you want, this should take care of the rest of the task.

(Personally, if it were my machine, I'd probably want to write all the output files to some separate directory, or at least keep the originals intact somehow, so I could confirm that worked okay before blowing away the originals...)


In reply to Re: Re: Re: Problem with Renaming script by graff
in thread Problem with Renaming script 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.