Need programmer type help: using regular expressions?

0 Members and 1 Guest are viewing this topic. Read 1609 times.

nathanm

Okay, so this is a little bit out there, but what the hey - I know there's some programmer type folks around here.  My mission is this:  I want to rename this list of files (actually it's much bigger than this) so that there's just a 14-digit number and where there are duplicate numbers I want to add a serial letter after each to differentiate them.  I have been unable to fashion a solution from my usual renamer utility, but it also has this "regular expressions" feature which is unfamilar to me.  The way I figure this is the only way to seek out duplicate strings.

So for example:

01890000001215 Hopfen 2.jpg
01890000001215 Hopfen Detai.jpg
01890000001215 Hopfen.jpg

I want to result in:

01890000001215a.jpg
01890000001215b.jpg
01890000001215c.jpg

How would I format a regular expression to solve this?  Any ideas?  Thanks much! :thumb:  Oh sure, researching this project has gone well beyond the time it would have taken me to just manually type what I wanted, but dang it I wanna learn how to automate this if possible.

01890000001082 Ferienhaus w.jpg
01890000001082.jpg
01890000001083 Ferienhaus r.jpg
01890000001084 Ferienhaus g.jpg
01890000001133 Ladegut.jpg
01890000001137 Ausschneideb.jpg
01890000001137 Werbeplakate.jpg
01890000001138 Ausschneideb.jpg
01890000001138 Werbeplakte.jpg
01890000001139 Ausschneideb.jpg
01890000001139 Fahnen.jpg
01890000001152 Trike 2.jpg
01890000001152 Trike.jpg
01890000001209 Blumentopf S.jpg
01890000001214 1217 1218 12.jpg
01890000001214 Gurken Tomat.jpg
01890000001215 Hopfen 2.jpg
01890000001215 Hopfen Detai.jpg
01890000001215 Hopfen.jpg
01890000001216 Gerste.jpg
01890000001217 Lupinen.jpg
01890000001218 Lupinen.jpg
01890000001219 Dahlien.jpg
01890000001230 Blumentopfe.jpg
01890000001231 Margeriten u.jpg
01890000001232 Rosen und So.jpg
01890000001301 1305 Stimmun.jpg
01890000001301 6141 Dunen .jpg
01890000001301 Trockenes Gr.jpg
01890000001302 Fruhlingsgr.jpg
01890000001303 Sommergras.jpg

bpape

  • Industry Participant
  • Posts: 4465
  • I am serious and don't call my Shirley
    • Sensible Sound Solutions
Re: Need programmer type help: using regular expressions?
« Reply #1 on: 6 Mar 2007, 10:55 pm »
Nevermind - misunderstood what was being asked.

Bryan
« Last Edit: 7 Mar 2007, 01:56 am by bpape »

jakepunk

Get your barf bags ready!
« Reply #2 on: 7 Mar 2007, 12:00 am »
Code: [Select]
#!/usr/bin/perl
# USAGE: $0 *.jpg

if (!@ARGV) {
        @ARGV = <STDIN>;
        chop(@ARGV);
}
for (@ARGV) {
        m/^(\d{14})/;
        $digits = $1;
        push(@{$files{$digits}}, $_);
}
for $digits (keys %files) {
        if (@{$files{$digits}} == 1) {
                rename(@{$files{$digits}}[0], "$digits.jpg");
        } else {
                for $index (0 .. @{$files{$digits}}-1) {
                        $suffix = (a .. z)[$index];
                        rename(@{$files{$digits}}[$index], "$digits$suffix.jpg");
                }
        }
}

I got off the Perl train around 4.0.36, so all you smarty-pants Perl 5 programmers: don't bother telling me about references or how hideous this is!

Papajin

  • Jr. Member
  • Posts: 276
Re: Need programmer type help: using regular expressions?
« Reply #3 on: 7 Mar 2007, 12:53 am »
Okay, so this is a little bit out there, but what the hey - I know there's some programmer type folks around here.  My mission is this:  I want to rename this list of files (actually it's much bigger than this) so that there's just a 14-digit number and where there are duplicate numbers I want to add a serial letter after each to differentiate them.  I have been unable to fashion a solution from my usual renamer utility, but it also has this "regular expressions" feature which is unfamilar to me.  The way I figure this is the only way to seek out duplicate strings.

Is there any documentation on how this regular expressions functionality actually works?  Looking at the problem, it would be fairly easy to pare the names down to just their 14-digit number and tack the extension back on the end, however the serialization could be a big problem depending on how exactly the program evaluates things.

For instance, if it evaluates each filename 1 at a time, then I'm not sure how you could go about serializing things with a single expression unless perhaps there's some added functionality that would allow for such a thing internal to your rename program.  If the whole list of filenames is evaluated as a single entity, then the renames are done after the fact, it may be possible to do what you're thinking.

It might be better to write a script of some sort to handle this task in the end.  Without more details about how the program processes the filenames, I'm not sure how I'd go about it.  If I were writing a script it'd be a piece of cake.  Maybe someone with more expertise in writing regular expressions will chime in though. :)

(I note that someone posted a perl script while I was typing this, which is more or less what I was thinking)

contrarian

  • Jr. Member
  • Posts: 65
Re: Need programmer type help: using regular expressions?
« Reply #4 on: 7 Mar 2007, 02:53 am »
I'm not sure what you are actually doing, but I'll take a stab - but please, not in the back.
javascript:void(0);
icon_lol

Regular expressions are looking for patterns in the string you present.  It sounds like you are not searching for "known" patterns in a single string, but looking for duplicates, which is only known by comparing current value with previous value.  If I understand this correctly, this technique won't work for you.  It will only flag your current string as a match or no match.  It won't be doing comparisons with previous values.

You might want to write code that will load your list, sort your list, loop through it, test your criteria and change the name accordingly.

What platform are you using? 


nathanm

Re: Need programmer type help: using regular expressions?
« Reply #5 on: 7 Mar 2007, 04:39 pm »
Thanks for the ideas!  It looks like doing it the "easy way" will probably be harder than doing it the "hard way" because like Contrarian said, I would have to tell it what to look for.  I should clarify that I'm not a programmer, I was only wondering about the syntax to use to find duplicates, but now that I think about it it's probably too complicated.  I am using an application called A Better Finder Rename which has all sorts of commands to find\replace stuff and "replace using regular expressions" is one of the abilities.  I guess I'm in over my head on this one!  heh!

Bob Reynolds

  • Full Member
  • Posts: 526
Re: Need programmer type help: using regular expressions?
« Reply #6 on: 7 Mar 2007, 05:15 pm »
Regular expressions are based on regular languages and, unlike context free languages, are stateless, i.e., they can not remember the details of what has already been processed.

So as has been stated already REs are not suited for this problem and scripting (or any programming language) would be necessary.

By all means, though, look into REs. They are a very powerful tool.


JohnR

Re: Need programmer type help: using regular expressions?
« Reply #7 on: 7 Mar 2007, 10:11 pm »
The File List Rename feature would probably be the easiest way to do it. An editor with regexp support could certainly make creating the list easier.

Or try jake's script (which does have a regular expression in it)...

jqp

  • Volunteer
  • Posts: 3964
  • Each CD lovingly placed in the nOrh CD-1
Re: Need programmer type help: using regular expressions?
« Reply #8 on: 17 Mar 2007, 04:45 pm »
do we get to see any of the german pics?