[brlug-general] batch rename

willhill williamhill2 at cox.net
Thu Nov 16 10:41:34 CST 2006


The perl program "no space" has always worked for me.  For renaming camera 
images and other numbered files gqview's batch rename and krename are both 
very powerful and easy to use.  Here's my copy of nospace.pl, you might want 
to look for a newer version.

#!/usr/bin/perl -w
# nospace /this/dir /that/dir /those/too 
# author tomw (Tom Willett) of pigsty.net
 
 use File::Find; 
 use strict; 
 die "usage: nospace dir[s]\n" unless @ARGV; 
 
 my %ext; 
 
 find(\&remspaces, @ARGV); 
 
 sub remspaces {
 return if ($_ eq '.'); 
 return if ($_ eq '..'); 
 (my $new = $_) =~ tr/a-zA-Z0-9_.-/_/c; 
 my $duplicate = ($new ne $_ and -e $new);
 my $try = $new;
 
 $ext{"$File::Find::dir/$try"}++ if $duplicate;
 
 while (my $count = $ext{"$File::Find::dir/$new"}++) {
 (my $with_num = $new) =~ s/(?=\.|$)/_$count/;
 $new = $with_num, last if not -e $with_num;
 }
 
 $ext{"$File::Find::dir/$try"}-- if $duplicate;
 
 rename $_ => $new
 or warn "can't rename $_ to $new: $!";
 } 

On Thursday 16 November 2006 09:30, Joe Fruchey wrote:
>... So I wanted to convert the names to all lowercase,
> change the spaces to underscores, and strip out the special
> characters.
>
> I was gonna post a message to the list about it, but I decided to
> search first, and here's what I found:
>
> http://filerenameutils.sourceforge.net/
> ... 



More information about the General mailing list