Page 1 of 1

Original Filename

PostPosted: Mon Feb 04, 2013 5:28 am
by gsomby
I batch rename my files to 'DDMMYY_HHMM_<org filename>'

Is there a quick way to rename them back to the original filename?
I have tried finding a way to do this with RegExp, but are not able to get it to work

/gunnar

Re: Original Filename

PostPosted: Tue Feb 05, 2013 3:47 am
by Axel
Yes, there is a way, but the renaming you are looking to perform is not trivial and cannot be accomplished using standard techniques. We will create a renaming template making use of a regular expression, and then show you how to use the template in the File Utilities batch processor.

There are many ways to split your file names, let look at two (both assume that the file names are consistent, i.e. all in the 'DDMMYY_HHMM_<org filename>’ you described).

Let’s assume that the <org filename> part of the base file name is always 8 characters (FastPictureViewer never renames the extensions, so the extension is not included in the rename feature at all).

What we want to do is capture the last 8 characters of the base file name, and use these as the new file name:

Code: Select all
{RegEx:.*(.{8})$}{1}

The above rename macro will do the trick. The {RegEx: }{} symbols are the syntactic sugar which make this text a regular-expression macro, what’s interesting is what’s inside the braces:

.* matches zero or more occurrence of any character
() delimites a capture group. Since this is the first pair of parentheses, the capture group will be numbered “1”.
Inside the parentheses, we see .{8} which means 8 occurrences of any character.
Right after the capture group we see a $ sign, which means “end of string”.

Then there is the replacement string, in the last pair of braces, with the number of the capture group that we saw before.

In one sentence, it means: skip all characters but the last 8, and use these as the new name.

Job done.

If the <org filename> part of the file name is of variable length, thankfully the “DDMMYY_HHMM_” prefix you used is well-defined and always 12 characters in length. We could use a very similar regular expression:

Code: Select all
{RegEx:.{12}(.+)$}{1}

This means “skip 12 chars and capture everything else remaining on group #1, then use that reminder as new file name”.

This would work but we can make the macro more selective, as we know the prefix is always made of 6 digits, followed by an underscore, then another 4 digits and a second underscore:

Code: Select all
{RegEx:\d{6}_\d{4}_(.+)$}{1}

It can easily be deduced that \d means “digit”. Since we know more about the data (the first 6 digits actually is a date in DDMMYY form, for example, we know that the first digit can only by 0, 1, 2 or 3, and we could take advantage of this knowledge to create an even more selective regular expression (left as an excercise...)

To strip your file name's prefixes, proceed as follow:

Press F2 to bring up the renaming dialog. In the middle field, pick “Edit Templates…”. In the first field of the windows that opens, pick “New…”. Invent a meaningful and unambiguous name, like “Strip DDMMYY_HHMM_ prefixes),then put the last of the 3 macros above in the “renaming rule” input field. Verify that the rue does exactly what you want in the “Resulting file name field”, then click OK to save the template and close the window. Your new template is now available in the middle field of the Rename dialog, and you are ready to rename your files semi-automatically one by one.

If you want to process many files at once, can create a renaming rule in the File Utilities plug-in simply made of a rename action: press Ctrl+F to bring up the File Utilities plug-in. Click New… at the top-right, check the “Rename using the specified template” in the second (middle) list, then, in the input field at the bottom, click the underlined “template” word, pick your template, give a meaningful name to the rule, save it and run it on all the open images.

If you want to make your renaming rule bullet-proof, you can also add a condition to it, which would be “Where the name matches a given pattern” that you’ll find in the upper part of the rule editor’s window. The pattern would be

Code: Select all
\d{6}_\d{4}_.+

which means 6 digits, one underscore, 4 digits, another underscore and one or more following characters.

Be sure to click the “Interpret the pattern as an ECMAScript Regular Expression” option in the condition editor!

Your rule should now read something similar to the following line:

Code: Select all
Where the file name matches \d{6}_\d{4}_.+ then rename it using the Strip DDMMYY_HHMM_ Prefix template.

This rule will only select the files with the specified prefix and strip it, which is exactly what you want.

Feel free to add more selection criteria’s (for example a date range) if you have more information about the files you need to rename.

Also note that it would be easy to make another rule that adds a prefix only to file not already having one (using the “except” part of the rule with the appropriate pattern). The possibilities are endless.

Try your rule on a single file first, then, once you are confident that everything is set up properly, you can go on and rename all your files at once (batch processing is available in the registered version).

The File Utilities batch processor keeps a log of all errors encountered. You can view and save the log after your batch command has finished running.

All of the above sounds like a lot of work but in fact it's quite simple, once you have done it once. The tricky part is the regular expression part, which is a bit hard to explainn and learn, but very powerful.