[conspire] Howto find replace change newline control character in filename script program perl
Bill Ward
bill at wards.net
Wed Dec 14 23:56:52 PST 2005
I'd add 'warn "$orig: $!" ' to your rename() but otherwise, looks good.
On 12/14/05, Tony Godshall <togo at of.net> wrote:
>
> How about this?
>
> find . -type f \
> | perl -ne '\
> chomp;
> my $oldname=$_;
> while(s{^(.*)[\r\n]+(.*)$}{$1$2}}) { }
> if ( $oldname == $_ ) { rename($orig,$_); }
> }
> '
>
> A little less complicated. And easily adapted for other
> purposes (I use something like this for cleaning up mp3
> filenames before dumping them to my iriver).
>
> According to Hereon,
> > Howto find replace change newline control character in filename script program perl for linux unix
> >
> > Thanks to Bill & Sam.
> >
> > Useful for fixing .html files saved by Mozilla firefox.
> >
> > I sometimes copy the title of an article and paste it to the save dialog box of Mozilla firefox.
> > Sometimes this contains a newline character that I don't notice.
> > K3b cd writing backup fails on the filenames containing newlines.
> >
> > Place the script below in a text editor & save it in /usr/local/bin
> > as FindReplaceNewlineCharacterInFilename.pl
> > chmod 755 FindReplaceNewlineCharacterInFilename.pl
> > Go to the top of the directory tree you wish to search & replace.
> >
> > Run the program
> > FindReplaceNewlineCharacterInFilename.pl
> >
> > Running it with no options lists filnames which contain newline characters.
> > Run it with the "-r" option to actually change the file names.
> >
> >
> > =====================================================================
> > #!/usr/bin/perl -w
> > #
> > # Usage: FindReplaceNewlineCharacterInFilename.pl [-r]
> > # Recursively finds all files and directories with newline characters in their name
> > # and optionally renames them to have spaces instead. By default, files are not renamed;
> > # the files that would be effected are listed on stdout. If invoked with the -r option,
> > # the files are renamed.
> > #
> > use File::Find; # Recursively visits every file in a list of directories
> > use Cwd; # for returning the current directory with getcwd().
> > use strict;
> > no warnings 'File::Find';
> >
> > $::DO_RENAME = $ARGV[0] eq "-r"; # Is true if user wants to actually rename the file
> >
> > my @dir;
> > push( @dir, getcwd() ); # Start in the current directory
> > find( \&RemoveUnsafeCharactersFromFilename , @dir );
> >
> > ##
> > # Replace all files with \n in them with spaces
> >
> > sub RemoveUnsafeCharactersFromFilename {
> >
> > # If the filename (that Find::File::find() puts in $_ and in $File::Find::name)
> > # contains a Newline character (\n, i.e. hex 0A ASCII 11 entered as ^V^J),
> > # replace it with a simple space character.
> >
> > # If the current filename contains the undesired control character,
> > if( m/[\n]/ ) { # m/pattern/ does a regex match of `pattern' against the $_ variable
> >
> > # Create string for user display that shows "/r"'s in the file name.
> > my $old_filename = $File::Find::name;
> > my $old_display_name = $old_filename; # show the old filename on a single line
> > $old_display_name =~ s/[\n]/\\n/g; # Put "\n" in the output to see.
> > s/[\n]/ /g; # s/old/new/ replaces `old' pattern with `new' value in the $_ variable
> >
> > # Show user the file name.
> > print $::DO_RENAME ? "Renaming " : ""; # Say "Renaming" if '-r' option
> > print "$old_display_name\n";
> > print $::DO_RENAME ? "To ===== $_\n" : ""; # Show new file name if renaming
> >
> > # We only rename files if the script is invoked with the -r option
> > if( $::DO_RENAME ) {
> > rename( $old_filename, $_ ) or die "Couldn't rename $old_display_name to $_: $!";
> > }
> > }
> > 1; # Return 1 just in case.
> > }
> > --
> > Hereon
> > hereon1 at fastmail.us
> >
> > --
> > http://www.fastmail.fm - Same, same, but different?
> >
> >
> > _______________________________________________
> > conspire mailing list
> > conspire at linuxmafia.com
> > http://linuxmafia.com/mailman/listinfo/conspire
>
> --
>
> Best Regards,
>
> Tony
>
>
> _______________________________________________
> conspire mailing list
> conspire at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/conspire
>
--
Help save the San Jose Earthquakes - http://www.soccersiliconvalley.com/
More information about the conspire
mailing list