Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to clean up a bunch of folder file names. Possibly even the file titles as well. I was currently using this Perl script to do so but I ran into some problems.

  1. If the file name already has parenthesis around the year, it adds another set.
  2. I would like to also use a text file to remove common "signatures" in titles as well, ex. This.Movie.Blah.(2012) [1080p] MP4.

    Currently the script I have replaces the . with a space as intended and adds (xxxx) around the year. Running the script the outcome would be This Movie Blah ((2012)) [1080p] MP4.

Also I would like remove the "signatures" that match a text files values (lined, not CSV) from the file name so This.Movie.Blah.(2012) [1080p] MP4 would return This Movie Blah (2012)

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;
closedir $d;

I had just started trying to learn Perl today as one of my first programming languages so I am relatively new to this language.

share|improve this question

As a question of regular expressions (searching text for patterns and doing replacements based on them), this is not hard; I’m surprised that there are no answers after 15 hours.  I’m not as experienced in Perl as I’d like to be, but a straightforward approach to the year-in-parentheses problem is simply to detect it afterwards and fix it then:

        $_ =~ s/(\d{4})/($1)/g;
        $_ =~ s/(\((\d{4})\))/$1/g;

The second statement looks for ((2012)) and returns the middle six characters; i.e., the (2012).  As to “common signatures”, how about

        $_ =~ s/ \[1080p\] MP4$//;

?   (Use \[ and \] to prevent […] from being interpreted as a range, and $ to refer to the end of the string.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.