ADD PREFIX ONLY IF FILE CONTAINS

Bulk Rename Utility How-To's

ADD PREFIX ONLY IF FILE CONTAINS

Postby dsenge85 » Sat Apr 11, 2020 5:03 pm

Hello,

It looks like I need to use REGEXP to make this work. I have never used that. I just use this tool to bulk replace usually.

I need to take all files in one folder and add a prefix based on the name,

so add prefix R if file contains "RAIL"
add prefix T if file contains "TAIL"
add prefix S if file contains "SAIL"
and so on

Sorry if this answer is already here. I am new to this. Thank you for any help.

Thanks
Don
dsenge85
 
Posts: 1
Joined: Sat Apr 11, 2020 4:53 pm

Re: ADD PREFIX ONLY IF FILE CONTAINS

Postby RegexNinja » Sat Apr 11, 2020 5:26 pm

Hi.. Pleae note that regex doesnt support hard-coded conditionals in its replacement.
But in this case, you could always match for ([A-Z])AIL and use \1 in the replacement:

#1 Regex Match/Replace
^(.*)([A-Z])(AIL.*)$
\2\1\2\3

Results like:
Begin_SAIL --------> SBegin_SAIL
TAIL_end ----------> TTAIL_end
Begin_RAIL_End --> RBegin_RAIL_End
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: ADD PREFIX ONLY IF FILE CONTAINS

Postby trm2 » Fri Apr 24, 2020 1:40 am

A simple modification to the RegEx will do it more universally;

Match: ^(.*)([A-Z])(.*)$
Replace: \2_\2\3

Handles all files - takes the last initial of last word and prefixes a _<initial>
If you don't want the underscore, just remove it in the Replacement String.

If you want the first word's initial instead, use this:

Match: ^(.*?)([A-Z])(.*)$

If you want uppercase or lowercase included change it to:

Match: ^(.*)([A-Za-z])(.*)$
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm

Re: ADD PREFIX ONLY IF FILE CONTAINS

Postby trm2 » Fri May 15, 2020 2:50 pm

Sorry - wrong Regex.

Corrected -

Match: ^(.*?)([A-Z])(?=[A-Z])(.*)
Replace: \2_\1\2\3

First word initial -


Match: ^(.*?)([A-Za-z])(.*)$
Replace: \2_\1_\2\3
trm2
 
Posts: 156
Joined: Wed Jan 15, 2020 12:47 pm


Return to How-To