Geoff said:
I thought rename did not apply to Perl with Windows? am I wrong?
The rename() function works just fine with Windows.
Whatever gave you the impression that it doesn't?
(If you are referring to perl's '-i' command line option with
Windows, that is a different subject.)
Assuming it does work do I have to?
1. rename orig files to new names
2. delete orig files
But if you've done a rename, the original file is no longer
there, therefore there is nothing for step 2 to delete.
3. rename new files to orig file names
What? You mean put everything back the same as it was?
Why do that if you are not going to change anything?
Cannot just rename files in one step?
Sounds like you are very confused.
That makes more sense. Just do step 4 in your original script.
==================================================
Here is how "perl -pi -e 's/foo/bar/g' *.txt" works with Unix:
1) Open input file (whatever.txt) for reading.
2) Delete the input file. (Can't do this with Windows.)
3) Open output file (whatever.txt).
4) Read lines from the input file one at a time.
5) Change all instances of 'foo' to 'bar'.
6) Write the modified line to the output file.
7) Repeat previous three steps until EOF.
8) Close the input file. If no processes still have it open
for reading, this is when the file is actually deleted.
9) Close the output file.
10) Repeat for all files specified on the command line.
Here is how 'perl -pi.bak -e "s/foo/bar/g" *.txt' works (Windows + Unix)
1) Rename filename to filename.bak (whatever.txt to whatever.txt.bak).
2) Open the renamed file for reading.
3-7) = same as above
8) Close the input file. The original file is still there, albeit
with a different name.
9-10) same as above.
Here's how to do those steps using File::Find
sub wanted {
my $fil = $_; # Name of input file, relative to current dir
my $tmp = "$fil.tmp";
rename $fil,$tmp or die "Cannot rename $fil to $tmp - $!\n";
open my $out,'>',$fil or die "Cannot write to $fil - $!\n";
open my $in, '<',$tmp or die "Cannot read from $tmp - $!\n";
while (defined my $line = <>) {
$line =~ s/foo/bar/g;
print $out $line or warn "Problem writing to $fil - $!\n";
}
close $out or warn "Problems closing $fil - $!\n";
close $in;
unlink $in or warn "Unable to remove $tmp - $!\n";
}
-Joe