Page 1 of 1

ADD PREFIX ONLY IF FILE CONTAINS

PostPosted: Sat Apr 11, 2020 5:03 pm
by dsenge85
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

Re: ADD PREFIX ONLY IF FILE CONTAINS

PostPosted: Sat Apr 11, 2020 5:26 pm
by RegexNinja
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

Re: ADD PREFIX ONLY IF FILE CONTAINS

PostPosted: Fri Apr 24, 2020 1:40 am
by trm2
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])(.*)$

Re: ADD PREFIX ONLY IF FILE CONTAINS

PostPosted: Fri May 15, 2020 2:50 pm
by trm2
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