Subject: Re: [Name Mangler Talk] find last "_", then delete everything before

From: Rob Griffiths <[email-masked]>
Date: Mon, 14 Apr 2014 08:03:23 -0700

On Apr 14, 2014, at 7:33 AM, we 3 <[email-masked]> wrote:

> 0103061196002999~back~PROCESSED.png
> 0103061196002999~front+1~PROCESSED.png
> 
> to:
> 
> 0103061196002999.png
> 0103061196002999.png
> 
> So not always the same ending string and not always the same length.

The removal is quite simple, but as you can see above, you'd wind up with non-unique filenames, so you'll need to add a unique sequence number, too. It looks like this:

That Find/Replace bit is: Find (.*)(~.*~.*), and Replace With with $1 (everything before the first tilde). Then a sequence number is added (use the "+" sign to add "Name" metadata to capture the reworked name).

regards,
-rob.

---

From: we 3 <[email-masked]>
Date: Mon, 14 Apr 2014 07:33:51 -0700 (PDT)

My example is:

0103061196002999~back~PROCESSED.png
0103061196002999~front+1~PROCESSED.png
0103061196002999~front+2~PROCESSED.png
0103061196002999~front~PROCESSED.png
0107023966000999~back+1~PROCESSED.png
0107023966000999~back~PROCESSED.png
0107023966000999~detail~PROCESSED.png
0107023966000999~front~PROCESSED.png

to:

0103061196002999.png
0103061196002999.png
0103061196002999.png
0103061196002999.png
0107023966000999.png
0107023966000999.png
0107023966000999.png
0107023966000999.png

Matt

On Monday, April 14, 2014 3:29:11 PM UTC+1, we 3 wrote:
>
> This is great.. What if I needed to remove everything after the 
> underscore? or in my case a ~ ?
>
> Many thanks
>
>
>
> On Wednesday, August 15, 2012 10:45:49 PM UTC+1, Many Tricks wrote:
>>
>> On Aug 15, 2012, at 7:19 AM, Nic Schultz wrote: 
>>
>> > I have a whole group of funky filenames and I'm trying to get rid of 
>> everything but the last chunk of information. This final part of the 
>> filename is always numeric, always the end of the filename and always 
>> preceded by an underscore. I'd like reduce these files' names to only 
>> contain this last block of numbers. So.... 
>> > 
>> > CENTER_POINT_HS_P100_5863.eps 
>> > CENTRAL_VALLEY_HS_P200_020939.png 
>> > COOLIDGE_HS_P800_021045.svg 
>> > 
>> > Should end up being 
>> > 
>> > 5863.eps 
>> > 020939.png 
>> > 021045.svg 
>> > 
>> > Thanks in advance for your help. If I can get this working, it will 
>> save me ALOT of time and make me look REALLY good at work. 
>>
>> *********************************************** 
>> Before I go forward, PLEASE PLEASE do not apply what I'm about to say to 
>> your only set of files! If you can, test on a duplicate set of the files, 
>> or (even better) a made-up bunch of empty text files with the same name. 
>> I'm reasonably sure the following will work, but there's no Undo for this 
>> -- and I'd hate it if your filenames got destroyed! 
>> *********************************************** 
>>
>> You can do this via Find and Replace and its support for regular 
>> expressions. See the image below to see how it handled the three sample 
>> filenames you provided. In text form, you want (make sure the "Query is 
>> regular expression" box is checked): 
>>
>> Find:    ^.*_(\d+)$ 
>> Replace: $1 
>>
>> Here's what's happening for Find and Replace. The first Find bit... 
>>
>> ^.* 
>>
>> ... finds all characters [.*] in the string, starting at the first one 
>> [^], until we see a pattern that matches ... 
>>
>> _(\d+)$ 
>>
>> ... which translates to an underscore that immediately precedes one or 
>> more digits [\d is a digit, and + means 'at least one of the preceding 
>> item'] at the end [$] of a filename. The \d+ is wrapped in parenthesis, 
>> which means "treat this value, i.e. those digits we just extracted, as a 
>> variable." In the case of Name Mangler, that variable is stored in $1, 
>> which is what we're replacing the filename with. 
>>
>> In short form, we find any number of characters that end with an 
>> underscore followed by only digits at the end of the filename, and replace 
>> all the characters in the filename with those same digits. 
>>
>> Make some sense? Regex is complicated, and I'm far from an expert myself 
>> -- I just muddle through with various RegEx web pages and empty TextEdit 
>> sample files when I'm given a challenge like this :). So my explanation may 
>> not be 100% accurate, but as seen in the image, it seems to do what you 
>> want -- assuming that your filenames always end in that pattern of 
>> underscore followed by number. 
>>
>> regards, 
>> -rob. 
>>
>> ---------------- 
>>
>>

---

From: we 3 <[email-masked]>
Date: Mon, 14 Apr 2014 07:29:11 -0700 (PDT)

This is great.. What if I needed to remove everything after the underscore? 
or in my case a ~ ?

Many thanks

On Wednesday, August 15, 2012 10:45:49 PM UTC+1, Many Tricks wrote:
> > Thanks in advance for your help. If I can get this working, it will save 
> me ALOT of time and make me look REALLY good at work. 
>

---

From: Rob Griffiths <[email-masked]>
Date: Wed, 15 Aug 2012 14:45:49 -0700

On Aug 15, 2012, at 7:19 AM, Nic Schultz wrote:

> I have a whole group of funky filenames and I'm trying to get rid of everything but the last chunk of information. This final part of the filename is always numeric, always the end of the filename and always preceded by an underscore. I'd like reduce these files' names to only contain this last block of numbers. So....
> 
> Thanks in advance for your help. If I can get this working, it will save me ALOT of time and make me look REALLY good at work.

***********************************************
Before I go forward, PLEASE PLEASE do not apply what I'm about to say to your only set of files! If you can, test on a duplicate set of the files, or (even better) a made-up bunch of empty text files with the same name. I'm reasonably sure the following will work, but there's no Undo for this -- and I'd hate it if your filenames got destroyed!
***********************************************

You can do this via Find and Replace and its support for regular expressions. See the image below to see how it handled the three sample filenames you provided. In text form, you want (make sure the "Query is regular expression" box is checked):

Find:    ^.*_(\d+)$
Replace: $1

^.*

... finds all characters [.*] in the string, starting at the first one [^], until we see a pattern that matches ...

_(\d+)$

... which translates to an underscore that immediately precedes one or more digits [\d is a digit, and + means 'at least one of the preceding item'] at the end [$] of a filename. The \d+ is wrapped in parenthesis, which means "treat this value, i.e. those digits we just extracted, as a variable." In the case of Name Mangler, that variable is stored in $1, which is what we're replacing the filename with.

In short form, we find any number of characters that end with an underscore followed by only digits at the end of the filename, and replace all the characters in the filename with those same digits.

Make some sense? Regex is complicated, and I'm far from an expert myself -- I just muddle through with various RegEx web pages and empty TextEdit sample files when I'm given a challenge like this :). So my explanation may not be 100% accurate, but as seen in the image, it seems to do what you want -- assuming that your filenames always end in that pattern of underscore followed by number.

regards,
-rob.

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

---

From: Rob Griffiths <[email-masked]>
Date: Wed, 15 Aug 2012 12:44:18 -0700

> Thanks in advance for your help. If I can get this working, it will save me ALOT of time and make me look REALLY good at work.

This can be done, and I believe I have a solution -- I'm just sanity-checking it with someone who is a LOT stronger at regular expressions than am I. Once I hear back from them, I'll post the solution here. (I don't want to post something that would actually rename your files incorrectly!)

regards,
-rob.