Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alexgilder

macrumors newbie
Original poster
Jul 25, 2015
10
1
So I have a fair few hundred files where various titles such as "Letter dated 15 March 2013 from the Secretary-General addressed to the President of the Security Council.pdf" or simply "12 April 2018.pdf". The common denominator is that the date is always in the title in that format.

Is there a way to batch rename them all to change the dates to a format that will allow them to be displayed in date order? I need to read the documents in order by their publication dates but since they were downloaded from various sources over a few days the date added/modified is no help.

Thanks!
 
That seems too complicated for any renaming app I've ever seen.

You could use a renaming app in conjunction with a number of Finder searches (43+however many years are involved).

For instance, you search the folder with all the files for "name contains 01", select the results with the day of the month being 01 and then prepend the file with "-01 " (notice the space). Repeat for 02 thru 31.

Next, search for "January" and prepend those with "-01"...February would be "-02" and so on.

Then search for "2010" and prepend those with "2010 " and so forth for the other years involved.

You'd end up with your two example files files being named "2013-03-15 Letter dated 15 March 2013 from the Secretary-General addressed to the President of the Security Council.pdf" and "2018-04-12 12 April 2018.pdf".
 
You are probably best off learning to write a script. If you do things like this a lot it will save you lots of time. Here is something that might do what you want. It assumes that all dates are in the format as you wrote them and does almost no error checking but maybe it will get you going.

Save it into a file and then run `perl script.pl` in the directory containing all your files. Make a backup first.

Code:
#!/usr/bin/perl

use feature qw(say);
use warnings;
use strict;

# the months in order
my @months = qw(January February March April May June July August September October November December);
# build a regexp to match the months - this could be more complicated if we
# had to e.g. match Jan as well as January but this works for the 2 examples
my $months_re = join('|', @months);

# open the current directory
opendir(D, '.') or die;
# go through each file
while (my $f = readdir(D)) {
    # skip hidden files
    next if ($f =~ m/^\./);

    # see if this file has a date in its name
    if ($f =~ m/
            (?<day>\d{1,2})
            \s+
            (?<month>$months_re)
            \s+
            (?<year>\d{4})
            /ix) {
        # filename with a probable date in  - we should probably do some
        # checking here that its realistic but we don't...

        # we need to convert the month name to a number so it sorts correctly
        my $month_number;
        for ($month_number = 0; $month_number <= $#months; $month_number++) {
            if ($+{month} eq $months[$month_number]) {
                last;
            }
        }
        # if we didn't find this month in the array we ignore this file
        if ($month_number > $#months) {
            warn("failed to parse month for \"$f\"\n");
            next;
        }
        # now we add 1 to move from 0 based to 1 based indexing
        $month_number++;

        # create the new file name
        my $new_f = sprintf("%04d%02d%02d - %s", $+{year}, $month_number, $+{day}, $f);

        # do the move
        say("moving $f to $new_f");
        if (! rename($f, $new_f)) {
            warn("failed to rename \"$f\"\n");
        }
    } else {
        warn("no date in \"$f\"\n");
    }
}
 
A better Finder Rename allows you to batch rename files. It has some general renaming templates, but also allows the use of Unix regular expressions for complicated naming rules.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.