Extract part of filename.

A swapping-ground for Regular Expression syntax

Extract part of filename.

Postby Tarkus » Fri Nov 29, 2013 7:40 am

Here is an example of a filename I want to extract a certain part of it, and strip everything else, and then just add a timestamp to the end of it:

File - Random Name don't want this - - 2.txt

I want to strip everything except Random Name, which, as the example suggests, is not static. So I need to extract that name to use in the replacement string. Also, the 2 at the end can be any one or two digit number. I want to turn this into the following:

Random Name - 1311262156.txt

That number will be the timestamp added to the end of the filename, which should be easy enough to do once I extract the part of the filename I want to keep.

Thanks.
Tarkus
 
Posts: 4
Joined: Fri Nov 29, 2013 7:18 am
Location: USA

Re: Extract part of filename.

Postby Stefan » Fri Nov 29, 2013 8:18 pm

So what is your rule?

Keep everything between first Space-Hyphen-Space and second Space-Hyphen-Space?

And then add a timestamp, OK. Bu which timestamp should that be?




.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Extract part of filename.

Postby Tarkus » Fri Nov 29, 2013 8:47 pm

I don't have a rule. That's what I'm trying to figure out. I want to only keep the part in red, called Random Name in my example. I want to get rid of everything else. I can then use the Auto Date function to add the date. So I'm looking for code that will extract that part of the filename, and remove everything else.

I should add that other than the number at the end, the rest of the filename is always the same. Only the red part and the number at the end changes from filename to filename.
Tarkus
 
Posts: 4
Joined: Fri Nov 29, 2013 7:18 am
Location: USA

Eeny, meeny, miny, moe

Postby truth » Sat Nov 30, 2013 6:52 am

Since no rule was spec'd, more Orig/NewName examples are needed to develop one.
Without that, its just guessing, unless 'Random Name' is more narrowly defined.
Always 2-worded? Always start Capitalized? etc.

Guess 1:
.*? - (.*[A-Z][^ ]*).* - - \d+$
\1

Guess 2:
.*? - ([A-Z][^ ]* [A-Z][^ ]*) .* - - \d+$
\1

Theres plenty more, but for more accurate guessing: Post more examples or more 'Random Name' details.
truth
 
Posts: 221
Joined: Tue Jun 25, 2013 3:39 am
Location: Earth, OrionArm, MilkyWay

Re: Extract part of filename.

Postby Tarkus » Sat Nov 30, 2013 8:05 am

Sorry, this regex stuff really confuses me. I'll try to be more clear on what I'm trying to do. Regarding Random Name, I've seen anywhere from one to four words, no specific case. I messed up, in that there isn't always a -counter at the end. Here are some examples:

Code: Select all
File - rush dont want this- - 2.txt
File - Pink Floyd dont want this- - 10.txt
File - liquid trio experiment dont want this-.txt
File - Emerson Lake and Palmer dont want this- - 7.txt


Forgetting the timestamps for a moment, I want to rename these to:

Code: Select all
rush.txt
Pink Floyd.txt
liquid trio experiment.txt
Emerson Lake and Palmer.txt


"File - " is always present at the beginning, and " dont want this-" always follows. I mean literally, if you need to use those strings. That text never changes. Then if there's a counter, it's " - n" tacked on, where n can be one or two digits.

Your Guess 1 worked in most of my tests. It failed on a non-capitalized name and when there was no counter.

Thanks for your help!
Tarkus
 
Posts: 4
Joined: Fri Nov 29, 2013 7:18 am
Location: USA

Match EverythingElse ?

Postby truth » Sun Dec 01, 2013 4:18 am

Did you copy/paste filenames examples, or just typo? (dont|don't, this-|this -)
It seems unlikely such text would be appended to filenames, so edit as needed.

The below matches all filename-structures in your examples.
Note: 'this' must be followed by only Spaces/Hyphens and then #s (if #s exists)

Guess3:
^File - (.+) (dont|don't) want this[ -]+\d*$
\1

We'll need more examples/details to match anything more specific.
Just post back if you need to, or have any questions.
truth
 
Posts: 221
Joined: Tue Jun 25, 2013 3:39 am
Location: Earth, OrionArm, MilkyWay

Re: Extract part of filename.

Postby Tarkus » Sun Dec 01, 2013 4:58 am

The new code works perfectly on all my files! Thank you very much for your help!
Tarkus
 
Posts: 4
Joined: Fri Nov 29, 2013 7:18 am
Location: USA


Return to Regular Expressions