Help me extend the life of my camera

A swapping-ground for Regular Expression syntax

Help me extend the life of my camera

Postby RealGrouchy » Mon May 18, 2009 6:29 am

Hi there,

I like logic, and I know how to say in English (and perhaps in formal logic) how I want a command to perform, but I don't know regular expressions per se.

Thread 84 helped me get about 98% of where I want to go, but I need to get that last two percent. Here's my conundrum:

I got a camera in late December 2006. Pentax Optio W20. Great camera. Versatile and portable. Waterproof. I highly recommend it. If I can get this renaming thing to work, I might even buy a W60 to reward Pentax for making such a great little gadget.

The problem is, like most consumer-grade cameras, it names images IMGP0001.JPG to IMGP9999.JPG, and after that rolls over to IMGP0001.JPG again. The camera's solution is to put it into a different folder, which is completely useless since I toss my photos around my hard drive six folders from Sunday.

So now I've only 300 or so photos left until it turns over. I don't want multiple files of the same filename on my hard disk--that's just asking for trouble.

So I can either buy another camera (from a different manufacturer that uses a different naming scheme, probably only buying myself another 10K-1 photos), or find a way to alter the naming scheme and not spend money unnecessarily on a new camera.

If I had left all my photos in their original filenames, I would be set:

Code: Select all
Replace: ^(IMGP)([0-9]{4})
With: \10\2
I could run it as many times as I like, it will only change the unchanged ones. Once I roll over on the camera, change \10\2 to \11\2 and I'm set to keep my files in order ad infinitum.

The problem is, I've renamed many of the files. Renaming "IMGP1021.JPG" to "Billy's hot sister.jpg" isn't a problem, but I've lately found it useful to retain the original filename and also rename the file, so I'd rename "IMGP6745.JPG" to "IMGP6745 - Billy's not so bad himself.JPG".

With the above formula, this extra information gets stripped off.

I was thinking about the following, but it re-applies the algorithm to files that have already been renamed, making them six and more digits long. I want a reuseable formula:
Code: Select all
Replace: ^(IMGP)([0-9]{4})(.*)
With: \10\2\3
I'm afraid that I might also have renamed some files "IMGP3215.JPG" to "Billy's information-wink wink-gets stripped off IMGP3215.JPG" but Picasa sucks at trying to find that so I'm not sure. There would only ever be one instance of IMGP in a filename.

I'm pretty sure that any file I've renamed would NOT have a number immediately following the four digits following IMGP, so I think I'd be looking for a formula that does the following (in English, not regexp):
Look for a file which:
1 - May or may not have text before the following expression
2 - Has the characters "IMGP"
3 - Has four digits immediately following the "IMGP"
4 - May or may not have a character that is NOT a digit following those four
5 - May or may not have additional characters after that non-digit character

And do this with it:
1 - Retain the text from step 1 earlier
2 - Retain the text from step 2 earlier
Put the digit 0 in here [1 after I've rolled over]
3 - Put those four digits back here after the 0
4 - Retain the text from step 4 earlier
5 - Retain the text from step 5 earlier
It's possible that I'm unable to do this, which is why I'm asking you regexp'erts instead of driving myself madder stabbing in the dark.

I suppose that I could also replace instances of IMGP with IMGO and it would still sort right for the next ten thousand photos, but that's just so... inelegant.

I also suppose I didn't need near as much detail as I put in here.

Much appreciated if you can help.

- RG>
RealGrouchy
 
Posts: 6
Joined: Mon May 18, 2009 5:44 am

Re: Help me extend the life of my camera

Postby GMA » Mon May 18, 2009 8:03 pm

Hi, RG:
Well, let me just tell you what expression I came up with (and what it can or can't do) and then you tell me if it works for you or if it needs some refining:

MATCH: (.*)(IMGP)([0-9]{4}(?![0-9]))(.*)
REPLACE: \1\20\3\4


The explanation of the four MATCH groups (most of it you already know, but anyway):

(.*) = Match any number of carachters (including zero, if no characters present) before the second group.
(IMGP) = Match "IMGP"; mainly it just acts as a delimiter for the first and third groups.
([0-9]{4}(?![0-9])) = Match any four consecutive numbers ONLY if they're not followed by a fifth number.
(.*) = Match any number of characters (including zero, if no characters present) after the third group.

This means that the expression will change, for example:

"IMGP1234.JPG" to "IMGP01234.JPG"
"Some Text IMGP1234.JPG" to "Some Text IMGP01234.JPG"
"IMGP1234 Some Text.JPG" to "IMGP01234 Some Text.JPG"
"Some Text IMGP1234 More Text.JPG" to "Some Text IMGP01234 More Text.JPG"

It also means that it won't affect any files that already have a fifth number added to the four number string.
Is that what you wanted? Because all the info you provided ended up being a little confusing :). I'm not sure if you also wanted to change some file you renamed as "Some Text IMGP1234.JPG" back to "IMGP1234 Some Text.JPG", to help the ordering of your files. If that's the case, you need to use a second RegEx to do that.
Cheers,

Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina

Re: Help me extend the life of my camera

Postby RealGrouchy » Mon May 18, 2009 10:46 pm

Yes! That is precisely what I wanted!

(And now that you mention rearranging the filename, I might think about that too, to improve consistency...)

I'm not at the right computer at the moment, but when I'm done testing/implementing this, I'll make a stop by the donations page.

Thank you so much!

- RG>
RealGrouchy
 
Posts: 6
Joined: Mon May 18, 2009 5:44 am

Re: Help me extend the life of my camera

Postby RealGrouchy » Tue Jun 02, 2009 2:10 am

It worked magnificently!

:P :!:

Took much less time to rename the ~8500 files than it took Picasa to update its listing of them!

As promised, I have made a donation, and I will speak highly (and longly) of BRU on my blog.

Thanks again!

- RG>
RealGrouchy
 
Posts: 6
Joined: Mon May 18, 2009 5:44 am

Re: Help me extend the life of my camera

Postby GMA » Tue Jun 02, 2009 6:29 am

Glad it worked well, RG :)
Cheers,

Gabriel.
GMA
 
Posts: 91
Joined: Sun Dec 02, 2007 1:30 pm
Location: Argentina


Return to Regular Expressions