[CLUE-Tech] Re: removing spaces from filenames

Jed S. Baer thag at frii.com
Wed May 19 22:54:34 MDT 2004


On Wed, 19 May 2004 22:48:08 -0400
Angelo Bertolli <angelo at freeshell.org> wrote:

> The command rename will do this for the first occurence, maybe 10 passes
> is enough...
> 
> >"Joe 'Zonker' Brockmeier" <jzb at dissociatedpress.net> wrote:
> >
> >>I think Jed may have been referring to this, but there are a number of
> >>variations on rename.pl (just do a Google search) that are insanely 
> >>useful for removing spaces from filenames -- I use that quite often
> >for >"fixing" multiple files created by other folks on Windows.
> >>
> >
> >No, I just rolled my own. I'll check on that one though. It's possible
> >it's even better.

FWIW, try this

 -- fixdosnames.pl --

#!/usr/bin/perl -w

sub fixnames {

  shift;

# get rid of URL-encoded characters

  s/\%[0-9]{2}/_/g;

# convert to lower, and get rid of punctuation marks

  tr/A-Z/a-z/;
  tr' -~!@#$%^&*(){}|\":;?<>,[]+='_'s;
  tr/'/_/s;

  s/_+/_/g;
  s/^_+//g;

  return $_
}

while (<>) {

  chomp;
  $oldname = $_;
  $newname = fixnames($oldname);

  if ($oldname ne $newname) {
    print "mv -- $oldname $newname\n";
    @sargs = ("mv", "--", "$oldname", "$newname");
    system(@sargs) == 0 or print
      "unable to move file $oldname: $?\n";
  }

}

usage: ls | fixdosnames.pl

Comment out the print statement if you don't want to see what's
happenning. The reason for the "--" option to mv is that I ran across some
files where the first character was a dash, so without the "--" option, mv
was treating the filename as an invalid argument.

jed
-- 
http://s88369986.onlinehome.us/freedomsight/

... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier



More information about the clue-tech mailing list