Greedy Matching

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Greedy Matching Wed May 01, 2013 10:58 pm • by niltz
Hi, I want to match filenames with the following possible patterns

Name.Date.Code.Junk.ext
Name.Code.Junk.ext

Where:
Name is anything (...)
Date is a 4 digit number (1)(1)(1)(1)
Code is a 4 digit number (1)(1)(1)(1)
Junk is anything (...)

I want to create a rule that will parse out the Name, Date, and Code and rename the file as follows
Name[.Date].Code.ext

So I created rule with the following custom patterns:
GreedyNameWithDateIfProvided = anything (...)
Code = 4 digit number (1)(1)(1)(1)
Junk = anything (...)

I then rename the file to
GreedyNameWithDateIfProvided.Code.ext

I was hoping that the first anything would be greedy and match as much as possible, but it doesn't seem to be. For files that look like Name.Date.Code.Junk.ext, GreedyNameWithDateIfProvided.Code.ext just matches the Name, and then the Code gets matched to the Date, and then the file gets renamed to Name.Date.ext without the Code.

I hope this all makes sense, and I hope there is a way to make my match be greedy.

Thanks.
niltz
 
Posts: 6
Joined: Wed May 01, 2013 10:41 pm

Re: Greedy Matching Thu May 02, 2013 11:38 am • by a_freyer
You are correct that matching is not greedy for the anything (...) flags. For word (ab) or number (12) etc, it is.

That said, you can do this (tested OK):

NOTE: You are perfectly fine using the strategy above and combining date and code for the first format into a single token. I didn't do that in case you'd like to use the date separately.

Code: Select all
if (any) of the following conditions are met for (the file or folder being processed):
     Full Name matches (• Name 1)(• Date).(• Code 1).(ab12).ext
     Full Name matches (• Name 2).(• Code 2).(ab12).ext

Do the following to the matched file or folder:
     rename with pattern (• Name 1)(• Name 2)(• Date).(• Code 1)(• Code 2)(extension)


Where:

• Name 1 = (...)
• Name 2 = (...)
• Date = .(1)(1)(1)(1) NOTE: place a period in front of this custom token; all other periods separate in the matching pattern main dialog
• Code 1= (1)(1)(1)(1)
• Code 2 = (1)(1)(1)(1)

Explanation:

Since you are matching (any) file or folder, both patterns will be searched. One matches files with the Name.Date.Code.Junk.ext format, the other matches Name.Code.Junk.ext format. Matching on full name is important here, because otherwise (12) that you insert for the 'junk' will match the end of the first format twice.

This way, when the first format comes through, all tokens are set, EXCEPT ones that correlate to the second pattern, which will be blank. Similarly a second format file will blank the first set of tokens, but fill the second set.

Name.2004.1233.junk.txt

• Name 1 = Name
• Name 2 =
• Date = .2004
• Code 1= 1233
• Code 2 =


Name.1233.junk.txt

• Name 1 =
• Name 2 = Name
• Date =
• Code 1=
• Code 2 = 1233
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Greedy Matching Sat May 04, 2013 7:21 pm • by niltz
Thanks, that seems to do the job!
niltz
 
Posts: 6
Joined: Wed May 01, 2013 10:41 pm


Return to Support