topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 6:43 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Chop off extra junk in file names  (Read 9976 times)

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Chop off extra junk in file names
« on: June 20, 2012, 05:10 PM »

Another little problem has come up....

I'm generating a bunch of contour plot images in a statistics program.  The resulting plot images are supposed to be named according to the script of commands as such:

_50501_22004_22404_.png
_50501_22004_22403_.png
_50501_22004_22402_.png
_50501_22004a_22401_.png
_50501_22004_22306_.png
_50501_22004_22305_.png
_50501_22004_22304_.png
_50501_22004_22303_.png
_50501_22004_22302b_.png
_50501_22004_22301_.png

I've deliberately set up this naming approach because of the problem I'm about the describe.

For some crazy reason the program creating the plot images adds numbers to the ends of the names:

_50501_22004_22404_3000.png
_50501_22004_22403_3001.png
_50501_22004_22402_3002.png
_50501_22004a_22401_3003.png
_50501_22004_22306_3004.png
_50501_22004_22305_3005.png
_50501_22004_22304_3006.png
_50501_22004_22303_3007.png
_50501_22004_22302b_3008.png
_50501_22004_22301_3009.png

I would like the original names, or, rather, the names as they were intended to be.

One solution to the problem is to create a batch file:

ren    _50501_22004_22404_3000.png    _50501_22004_22404_.png
etc.

The total number of files is in the tens of thousands.  Over repeated times.  I believe all the underlying names are unique (no repeats).

I'm wondering if any of you can come up with a better approach than creating a basic batch file to chop off that extra junk after the third underline character, but, of course, before the file name extension.

Or, chop junk BETWEEN third underline character and period of the file extension.

Some of you might come up with a magic RegEx solution?  Please then suggest, too, a RegEx program.

Or maybe one of you will create a small program to do the trick?  Hopefully generalizable so that others can use the program for their specific needs.

Not sure.  If no better solution than the creating basic batch file approach, I'll have to continue using that.

Thanks much!

Nicholas Kormanik
[email protected]
.


jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #1 on: June 20, 2012, 05:42 PM »
I am not running windows, so I can't really program something to fully solve your problem.
I can, however, give you a matching regex that should do it:
^(_[^_]+_[^_]+_[^_]+_).*.png$
and the replace pattern should be:
$1.png
Depending on the program you use, "$1" could also be "\1" and some of the special characters such as "()+[]" may require escaping such as "\(" instead of "(".
The example I gave should work for the first google hit for "regex renamer" which actually looks pretty neat (even though I've never tried it).
« Last Edit: June 20, 2012, 05:57 PM by jgpaiva, Reason: oops, regex error :-[ »

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #2 on: June 20, 2012, 06:54 PM »
My favourite renamer is ReNamer found here: http://www.den4b.com...&product=renamer

1) Download and run.
2) Add the files/folders you want.
3) Add a RegEx rule like this:

2012-06-20_185142.pngChop off extra junk in file names

4) You should see the preview change to this:

2012-06-20_185257.pngChop off extra junk in file names

If you can't tell from the screenshot what the RegEx pattern is, here you go: (_.*)_.*

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #3 on: June 20, 2012, 07:44 PM »
jgpaiva, nice find, that RegexRenamer.  Nick, if you choose to go with that application, here's a screenshot to help you along:

2012-06-20_194531.pngChop off extra junk in file names

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #4 on: June 20, 2012, 08:17 PM »
jgpaiva, nice find, that RegexRenamer.  Nick, if you choose to go with that application, here's a screenshot to help you along:
 (see attachment in previous post)

That does look interesting. I'm going to try it out myself.

Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #5 on: June 20, 2012, 09:04 PM »
pretty much any of the 'good' renaming apps will do this for you out of the box - everyone has their preferred option but you could try BRU

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #6 on: June 20, 2012, 10:21 PM »
+1 from me for BRU (Bulk Rename Utility) for this. I had a similarly rather complicated renaming problem, and my usual problem-solver (xplorer²) couldn't help. I used BRU after reading a post about it on the DC forum.

WARNING:
Using BRU makes my brain hurt, but it's a very powerful tool. You could really screw up with it big time too.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #7 on: June 20, 2012, 11:56 PM »

To all, thanks for your help.  I made a mistake in my original post:

Chop junk between third underline character and period of the file extension.

Should read:

Chop junk between FOURTH underline character and period of the file extension.

_50501_22004_22305_3005.png

i.e., the 3005 part.

Hate to bother you, but would you please adjust your RegEx?

I've downloaded all three RegEx programs and will experiment when you provide the corrected code.


Target

  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 1,832
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #8 on: June 21, 2012, 12:16 AM »
I've downloaded all three RegEx programs and will experiment when you provide the corrected code.

FYI, if you use a renamer like BRU (and there are many) they already have the funtionality to do that, ie no regex needed.

The plus is that you can see, in real time, what your changes are going to look like before you make them, so there's plenty of time to fine tune your parameters

Screenshot - 21_06_2012 , 15_14_31.pngChop off extra junk in file names

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #9 on: June 21, 2012, 12:45 AM »
I've mentioned it before, but FreeCommander has a nice renamer. It's less complex than BRU but a lot better than Explorer.  For instance in the example given on RegexRenamer site about changing auto-generated filenames from a camera to a numbered sequence starting with MyPics or whatever it was.. in FreeCommander renamer you would just type the name as in MyPics[c]  then click on starting number and if to pad with how many zeroes etc..  Or if you want to keep the name and add to it you use [n] for name and [e] for extension in the extension part.  It cannot do things like BRU and remove letters from n'th position to n'th + 10 but it can do other things in a much simpler way.

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,149
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #10 on: June 21, 2012, 02:28 AM »
Please note that Nicholas don't want to remove the last underscore, e.g. there are only 4 signs to remove.

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #11 on: June 21, 2012, 02:35 AM »
Thanks AbteriX for pointing that out.  Yes, I want the last underscore to remain.

As for what's between the last underscore and the period, it's variable -- from one to six digits.  That's really the crux of the problem.  The 'junk' varies in length.  Need to cut out that junk.

Probably a magic RegEx code is called for.  Look forward to the RegEx gurus coming back with updated code (sorry I made the mistake about which underscore...).



jgpaiva

  • Global Moderator
  • Joined in 2006
  • *****
  • Posts: 4,727
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #12 on: June 21, 2012, 02:49 AM »
Hi, for ReNamer to keep the last underscore, you can use: (_.*_).*

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,149
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #13 on: June 21, 2012, 03:08 AM »
Nicholas, since the RegEx differs from tool to tool,... do you have an favorite now?

An RegEx for scarabee's Siren Renamer would be:
%b(s/(.+_).+/$1/).%e


- - -


Note that you can use also f.ex. den4b's ReNamer with an Rearrange Rule like this:

FROM:
_50501_22004_22404_3000.png
or
_50501_22004_22404_300012345678.png

TO:
_50501_22004_22404_.png

USE:
ReNamer Rearrange Rule:
Split by delimiters "_"
New pattern "$1_$2_$3_$4_"
(skip extension)

See > http://www.den4b.com...amer:Rules:Rearrange

Also note that ReNamer has an Analyze Dialog to test such issues without the need to have the file names in real on your disk:
http://www.den4b.com...wiki/ReNamer:Analyze

nkormanik

  • Participant
  • Joined in 2010
  • *
  • Posts: 552
    • View Profile
    • Donate to Member
Re: Chop off extra junk in file names
« Reply #14 on: June 21, 2012, 03:15 AM »

den4b's ReNamer looks really good.  I still would like to try the others, though.