[CLUE-Tech] removing spaces from filenames?

Adam A. G. Shamblin signal9 at earthlink.net
Mon Nov 18 21:18:25 MST 2002


Here's a script I patched together for fixing up downloaded mp3s.  Perhaps it's not the most elegant, but it saves a few keystrokes:

#!/usr/bin/perl -w
#
# namefix
#
# - June, 2002
#
# The purpose of this script is to rename downloaded .mp3 files
# to a general naming convention, and to remove the illegal characters
# and spaces often introduced by Windoze users to filenames.

use strict;
use File::Copy;

opendir CURRENT, $ENV{PWD} || die "Couldn't open $ENV{PWD}: $! \n";
my @pwd = grep { !/^\.\.?$/ } readdir CURRENT;
closedir CURRENT;

my ($count) = 0;

foreach my $file(@pwd){

    my($newfile) = lc $file;             # Lower case all file names

    $newfile =~ s/ /_/g;                 # Replace spaces w/ underscores

    $newfile =~ s/\'//g;                 # Remove apostrophes

    $newfile =~ s/&/and/g;               # Replace ampersands

    $newfile =~ s/(\(|\))//g;            # Remove parenthesis

    $newfile =~ s/(^|_)(\w)/$1\U$2/g;    # UC first letter of each word

    unless($file eq $newfile){
	move($file, $newfile);           # Rename file
	$count++;
    }
    
}

print "$count filenames adjusted.\n";
    

-- 
Adam A. G. Shamblin	<signal9 at earthlink.net>

And they shall beat their swords into plowshares,
for if you hit a man with a plowshare, 
he's going to know he's been hit.




More information about the clue-tech mailing list