Script to clean up folder names

I have a lot of folders that need to be renamed based on couple rules. example of folders:

-- Some.Folder.Name.Today.2009
-- Another.messed.Up.folder.1980
-- Third.messed.Up.folder.1980

I would like to see if anyone has a quick script to rename each folder to remove dot (.) from the folder name and also place parenthesis around year so it would look like this

-- Some Folder Name Today (2009)
-- Another messed Up folder (1980)
-- Third messed Up folder (1980)

Thanks a lot!


Perl handles this well, something like this should suffice:

use warnings;
use strict;
use File::Copy;

my $oldname;

opendir(my $d, ".") or die $!;
while(readdir $d) {
    if (-d $_ and $_ ne "." and $_ ne "..")
    {
        $oldname = $_;
        $_ =~ s/\./ /g;
        $_ =~ s/(\d{4})/($1)/g;
        move($oldname,$_);
    }
    }
closedir $d;

Only tested briefly, feel free to golf/modify it! It is not perfect by any means. Hacked it together quickly ;)

Note: In it's current state, it should be run form inside the directory with the mangled folder names.

C:\Users\John\Desktop\folders>dir
 Volume in drive C has no label.
 Volume Serial Number is 8888-1666

 Directory of C:\Users\John\Desktop\folders

29/01/2011  07:14 PM              .
29/01/2011  07:14 PM              ..
29/01/2011  07:07 PM              some.folder.name.2008
29/01/2011  07:07 PM              some.folder.name.2009
29/01/2011  07:16 PM               282 ren.pl
               1 File(s)            282 bytes
               4 Dir(s)  53,349,425,152 bytes free

C:\Users\John\Desktop\folders>ren.pl

C:\Users\John\Desktop\folders>dir
 Volume in drive C has no label.
 Volume Serial Number is 8888-1666

 Directory of C:\Users\John\Desktop\folders

29/01/2011  07:16 PM              .
29/01/2011  07:16 PM              ..
29/01/2011  07:07 PM              some folder name (2008)
29/01/2011  07:07 PM              some folder name (2009)
29/01/2011  07:16 PM               282 ren.pl
               1 File(s)            282 bytes
               4 Dir(s)  53,349,425,152 bytes free

I'd suggest running the filenames through a short sed script. for file in dir/with/files/*; do name=$(basename "$file" | sed 's/\./ /g;s/\([12][09][0-9][0-9]\)/(\1)/'); mv "$file" "$(dirname \"$file\")/$name"; done. Replaces the dots with spaces, then surrounds the year with parentheses.

Make sure that if you are scanning/operating on these files, that they are properly quoted. Spaces are often used as delimiters between items.


In PowerShell (which I believe is installed by default on Win7, although I don't have a Windows machine so I might be wrong),

gci *.* | %{
 mi $_ ($_.name.replace('.', ' ').insert($_.name.lastindexof('.')+1, '(')+')')
}