Subject: Re: Truncating and renaming a batch of irregular filenames.

From: Many Tricks <[email-masked]>
Date: Mon, 12 Dec 2011 06:15:07 -0800 (PST)

Matt:

Peter just noticed a tiny mistake in his original code. In this bit...

"01Jan 02Feb 03Mar 04Apr 05May 06Jun O7Jul 08Aug 09Sep 10Oct 11Nov
12Dec"

...what looks like "zero seven July" is actually written as "oh seven
July." So if you haven't used this yet, change the O7 to 07 first.

-rob.

---

From: Matt Spaeth <[email-masked]>
Date: Sat, 3 Dec 2011 08:38:26 -0800 (PST)

Wow! Thank you so much! 

Matt

---

From: Rob Griffiths <[email-masked]>
Date: Sat, 3 Dec 2011 08:26:37 -0800

On Dec 2, 2011, at 11:09 AM, Matt Spaeth wrote:

> I need to rename a bunch of filenames that are irregular in term and length, however they have a regular suffix. 
> 
> Example original filenames: 
> Aquaponics with Sylvia Berstein - E52 - Aug 24,2011.mp3
> Basics for Healthy Eating with Nutritionist Joni Soule - E54 - Sep 14,2011.mp3
> 
> Desired output: 
> FIN-E52-082411.mp3
> FIN-E54-091411.mp3
> 
> Is this possible with this software? 

It _is_ possible, though quite complicated. I had to turn to Peter Maurer, our founder and programmer, for the solution, as it requires some advanced regular expression magic and Name Mangler's advanced mode. Everything below the line is his answer. As always, _please_ test with a copy of your files to make sure this does what you expect.

---------------------------

Yes, that is indeed possible. If you're also interested in _how_ to do it, read on. ;) And please make sure you don't stop reading until you've also seen the notes near the end.

This one was a real noodle scratcher, mostly because I had to find a way to make Name Mangler digest shortened month names like "Aug". Here's the Advanced mode code you'll need:

[concatenate
	"FIN-",
	[findRegularExpression
		"^.+\s-\s(\D+\d+)\s-\s(\D{3})\s+(\d{1,2}),\d{2}(\d{2})$"
		in <name>
		replace with "$1"
	],
	"-",
	[findRegularExpression
		[concatenate
			".*(\d{2})",
			[findRegularExpression
				"^.+\s-\s(\D+\d+)\s-\s(\D{3})\s+(\d{1,2}),\d{2}(\d{2})$"
				in <name>
				replace with "$2"
			],
			".*"
		]
		in "01Jan 02Feb 03Mar 04Apr 05May 06Jun O7Jul 08Aug 09Sep 10Oct 11Nov 12Dec"
		replace with "$1"
	],
	[findRegularExpression
		"^.*(\d{4})$"
		in [concatenate
			"0",
			[findRegularExpression
				"^.+\s-\s(\D+\d+)\s-\s(\D{3})\s+(\d{1,2}),\d{2}(\d{2})$"
				in <name>
				replace with "$3$4"
			]
		]
		replace with "$1"
	],
	[if
		<extension>
	then
		[concatenate ".", <extension>]
	else
		""
	]
]

At the end of this messages is a screenshot showing what the result looks like.

Notes:

	- Your sample file names didn't include a single-digit day of month, so I didn't know whether a given original file name would have a "1" or a "01", for instance. The code converts "1" to "01", if need be (cf. "Test A").

	- Those sample file names were similarly opaque with regard to the "E..." part, which probably identifies an episode number. The code doesn't automatically convert "E1" into "E01", for instance (cf. "Test A"), because two digits struck me as too few to catch all possible values -- after all, we already are in the 50s (or possibly higher), so episode 100 may not be too far off (cf. "Test B"). If you expected different behavior in this regard, let us know.

	- This solution will cause trouble if some of the files you try to rename don't actually fit the original file name pattern you gave us. So make sure you check the "output" file name previews. And if that is overly inconvenient, e.g., because you plan on using this operation regularly on a folder structure that contains a mix of new files and already-renamed ones, do let us know, and we'll add a sanity check of sorts.

I hope this helps! Cheers,

Peter.

----------------------

Please feel free to post any additional questions back to the list.

-rob.

---

From: Matt Spaeth <[email-masked]>
Date: Fri, 2 Dec 2011 11:09:49 -0800 (PST)

I need to rename a bunch of filenames that are irregular in term and 
length, however they have a regular suffix. 

Example original filenames: 
Aquaponics with Sylvia Berstein - E52 - Aug 24,2011.mp3
Basics for Healthy Eating with Nutritionist Joni Soule - E54 - Sep 
14,2011.mp3

Desired output: 
FIN-E52-082411.mp3
FIN-E54-091411.mp3