Before I possibly bring untold damage upon a whole directory structure ...
what do the Perl gurus think of the code below? (In limited testing it has
worked as expected. It ignored pictures, located only Mac files, made a
backup of the original before modification, and the kept file dates.)
I seem to remember that Perl under Cygwin has defined \n (at least in some
contexts) to be CR/LF... So caveat emptor if anyone tries this under
Cygwin.
Paul
#!/usr/bin/perl -w
use strict;
use File::Find;
@ARGV = (".") unless @ARGV;
sub ChangeMac {
if (-T) { # Only process text files.
local $/; # To slurp the whole file into a scalar.
my ($FH, $OUT); # Local file handles.
my ($atime, $mtime) = (stat($_))[8,9]; # Store atime and mtime to
restore later.
open $FH, "<", $_ or die "Error opening file for input. $!\n"; #
Open the file
my $data = <$FH>; # Read the whole file into the scalar.
close $FH; # Close the file
if ($data =~ m/\r(?!\n)/) { # If we find it uses a Mac line
terminator
print "Processing ", $File::Find::name, "\n"; # Print the file
name we are processing
open $OUT, ">", $_ . '.bak' or die "Error opening backup file.
$!\n"; # Make a backup of the original
print $OUT $data;
close $OUT;
$data =~ s/\r(?!\n)/\n/g; # Change Mac line terminators to Unix
line terminators
open $OUT, ">", $_ or die "Error opening file for output.
$!\n"; # Write out the new file.
print $OUT $data;
close $OUT;
utime($atime, $mtime, $_ . '.bak') or warn "Couldn't restore ",
$File::Find::name, ".bak to original file's times: $!\n";
}
utime($atime, $mtime, $_) or warn "Couldn't restore
$File::Find::name to original times: $!\n";
}
}
find (\&ChangeMac, @ARGV);
exit 0;
--
You received this message because you are subscribed to the Google Groups
"NLUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nlug-talk?hl=en