0

I currently have a large set of directories containing photos. The directories are currently named in the format of yymmdd_text. I would like to rename them all to yyyy-mm-dd Text (notice capitalized text).

I have tried making a workflow using Automator but the rename Finder items function there is quite limited.

0

Save the code below into a text file (like "renameDir.pl"). Then open a Terminal windows where your folders are, and run the script like renameDir.pl 14* (or whatever pattern will match the folders you want renamed).

#!/usr/bin/perl

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

foreach my $dir (@ARGV) {
  (my $year, my $month, my $day, my $text) = ($dir =~ m/^(\d\d)(\d\d)(\d\d)_(.*)/);
  if ($year && $month && $day && $text) {
    my $Text = ucfirst($text);
    my $newDir = "20$year-$month-$day $Text";
    print "Moving \"$dir\" to \"$newDir\"\n";
    move($dir, $newDir) or die "Unable to rename $dir:\n$!\n";
  }
  else {
    warn "Unable to parse $dir\n";
  }
}

Assumes that all of your new folder names want to start with a year like 20xx, not 19xx, but you can edit the script to handle that as needed.

  • Two questions: This looks like it's creating a new directory and moving the files into it, I just wanted the directories renamed. Am I misunderstanding how this works? Also, I don't know perl but this looks like you missed the capitalization part. Don't you want something like $Text = capitalize($text) followed by my $newDir = "20$year-$month-$day $Text";? – Scott Wilton Aug 30 '14 at 21:44
  • You're right about the capitalization. Swap 'my $newDir = "20$year-$month-$day $text";' for 'my $newDir = "20$year-$month-$day " . uc($text);'. This does rename the dir; it doesn't make a new dir and copy the files. Test it out on a copy of one of your dirs. it definitely worked for me. – jimtut Aug 30 '14 at 21:53
  • And that should be ucfirst(), as the edited answer shows, not just uc() as my Comment above shows. That's what I get for answering on my phone and not thinking before I respond. :-) – jimtut Aug 30 '14 at 23:46
0

Couldn't you just use the cp command to copy and rename your directory for instance?

cp /home/yymmdd_text/* /home/yy-mm-dd_text/

  • The whole point is that it needs to be a batch command. The accepted answer above worked great. – Scott Wilton Aug 31 '14 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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