Replacing file names based on keywords found within a string

A swapping-ground for Regular Expression syntax

Replacing file names based on keywords found within a string

Postby Shawnny Canuck » Wed Jul 30, 2014 8:57 pm

Hello,

I apologize if this question has been asked already but I searched the forums and cannot find an answer. I am trying to rename files based on keywords that can be located anywhere within the file name and they can also be in lower or upper case and have an s at the end of each word. For example,

Original Name:

cow goes moo or
Cows Go Moo

Basically, once they keyword is located (in this case cow or Cows) within the string I want the file name to be replaced with a completely new word.

Desired Name:

Barnyard Animal

I figured I can spend days trying to research this problem or I could ask one of you regular expression experts who can figure it out in mins. Sorry for my terrible example but the files I am working on are sensitive in nature so I cannot share the actual file names. Thanks in advance for your help.
Shawnny Canuck
 
Posts: 18
Joined: Wed Jul 30, 2014 7:15 pm

Re: Replacing file names based on keywords found within a string

Postby Stefan » Thu Jul 31, 2014 7:21 am

I don't understand your question.

Your "cow" has no trailing "s" and so can't be detected as by your description.
located anywhere within the file name and they can also be in lower or upper case and have an s at the end of each word


- - -

I can only imagine you mean to detect "cow" or "cows" and replace that with a new string.

FROM:
cow goes moo.ext
Cows Go Moo.ext
Do the Cows Moo.ext
Here goes the cows.ext

TO:
Barnyard Animal goes moo.ext
Barnyard Animal Go Moo.ext
Do the Barnyard Animal Moo.ext
Here goes the Barnyard Animal.ext

RULE:
detect non-case sensitive Cow or Cows and replace with new string

USE:
RegEx(1)
Find: (.*)(c|C)ows*(.*)
Replace: \1Barnyard Animal\3


EXPLANATION:
(C|c) => find either upper OR lowercase "c"
ow => find literal "ow"
s* => find null-or-more "s" => match if "s" is there-or not.


Instead of (C|c) we can also use the regex "lower case" switch (?i):
Find: (?i)(.*)cows*(.*)
Replace: \1Barnyard Animal\2



Shawnny Canuck wrote:Hello,

I apologize if this question has been asked already but I searched the forums and cannot find an answer. I am trying to rename files based on keywords that can be located anywhere within the file name and they can also be in lower or upper case and have an s at the end of each word. For example,

Original Name:

cow goes moo or
Cows Go Moo

Basically, once they keyword is located (in this case cow or Cows) within the string I want the file name to be replaced with a completely new word.

Desired Name:

Barnyard Animal

I figured I can spend days trying to research this problem or I could ask one of you regular expression experts who can figure it out in mins. Sorry for my terrible example but the files I am working on are sensitive in nature so I cannot share the actual file names. Thanks in advance for your help.



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

Re: Replacing file names based on keywords found within a string

Postby Shawnny Canuck » Thu Jul 31, 2014 7:58 pm

Stefan,

I greatly appreciate your help and I am sorry for doing such a terrible job of explaining my problem I don't think it helped that I used such a poor example but you figured out exactly what I am looking to do. Also, thanks for not just providing the solution but explaining how each part of the regular expression works.

Like you mentioned I was trying to detect the word cow or cows within a string so the word is sometimes a plural and can be located anywhere within the original file name due to a large amount of people creating these files and adding their own naming convention. I was also trying to demonstrate how the word or words (in this case cow) can sometimes be upper and lower case so I needed a solution that wasn't case sensitive.

The one thing I wanted to add is I don't want to insert the replacement word into the string I want the replacement word to become the new file name so after testing out your solution I just put Barnyard Animal as the replacement and I got the result I wanted. Is it really that simple or am I overlooking something? Anyway, it appears your solution works and once again I really want to thank you for helping me solve this problem so quickly.

Lastly, since I have you here do you know if BRU can be run in batches if I need to find and replace a large number of files using different keyword criteria?
Shawnny Canuck
 
Posts: 18
Joined: Wed Jul 30, 2014 7:15 pm

Re: Replacing file names based on keywords found within a string

Postby Stefan » Fri Aug 08, 2014 10:42 pm

 
>>Is it really that simple
Yes, it is. But as I see this, you will end up with a lot of same file names, if you all just rename to 'Barnyard Animal'

>>> BRU can be run in batches if I need to find and replace a large number of files using different keyword criteria?
Maybe you can utilize the command line tool Bulk Rename Command ? See the home page.




Please wait a few seconds then submit. Thank you.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: Replacing file names based on keywords found within a string

Postby Shawnny Canuck » Tue Aug 12, 2014 4:48 pm

You’re right Stefan lots of duplicate names so my work around is to use a plug-in called AutoBookmark to process the files for renaming. It has batch capabilities and accepts RegEx and you can reuse the same names as bookmarks. I will look into the command line tool thanks for the suggestion. Once again thanks for your help.
Shawnny Canuck
 
Posts: 18
Joined: Wed Jul 30, 2014 7:15 pm

Re: Replacing file names based on keywords found within a string

Postby Stefan » Tue Aug 12, 2014 6:32 pm

Shawnny Canuck wrote:You’re right Stefan lots of duplicate names


I don't know if that will help you or if it is fine as it is now already,... but BRU has a option to prevent duplicates:

help wrote:Options Menu
Prevent Duplicates

This option allows to you to overcome the situation whereby a rename would fail because a file with the same name already exists.
If you try to rename a file, and there's already a file with the same name, the software will make up subsequent attempt to rename the file but with a "_1" suffix.
If this fails it will try with "_2" as the suffix, and will continue up to "_99". The limit of 99, and the separator character (underscore, _) are currently fixed and cannot be changed.






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


Return to Regular Expressions