2

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!

2
  • 2
    What is your operating system? Jan 30, 2011 at 0:03
  • So sorry I forgot to mention I'm running Windows 7
    – McLovin
    Jan 30, 2011 at 0:19

3 Answers 3

1

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
1

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.

2
  • 1
    Judging by 'batch' in the tags, I think his OS might be Windows. That said, he could still use the GNU tools on Windows, I guess. Jan 30, 2011 at 0:04
  • yes I'm running on windows 7, could you modify your script to do the same thing on folders
    – McLovin
    Jan 30, 2011 at 0:20
1

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, '(')+')')
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .