Simple renaming - remove (eXX) from file name

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Simple renaming - remove (eXX) from file name

Postby mikado86 » Sun Mar 29, 2020 8:23 pm

Hello, im pretty new to Java, can anyone give me some pointers?

I have different files, example:
(e85)RandomName1.txt
(e86)RandomName2.txt

What i want is:

RandomName1.txt
RandomName2.txt

I know how to do this basic thing while using the match and replace, but i have to do this constantly, and with many more variables, so it gets tiresome, can anyone point me in the right direction to make a javascript with my current needs?

Thanks
mikado86
 
Posts: 5
Joined: Sun Mar 29, 2020 8:16 pm

Re: Simple renaming

Postby mikado86 » Sun Mar 29, 2020 8:27 pm

Oh yeah, forgot to mention, it *might* always have (e/RandomNumber/RandomNumber) at some point in the sentence, not necessarily at the beginning.
mikado86
 
Posts: 5
Joined: Sun Mar 29, 2020 8:16 pm

Re: Simple renaming

Postby Admin » Mon Mar 30, 2020 12:30 am

Hi, what is the criteria? Removing everything that is part of a name within ( ) and including () ? thanks
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Simple renaming

Postby mikado86 » Mon Mar 30, 2020 8:07 am

Admin wrote:Hi, what is the criteria? Removing everything that is part of a name within ( ) and including () ? thanks


That is somewhat correct, i want to remove everything that BEGINS with "(e" (letter "e" will remain constant, always), then has two random numbers (they will always be numbers, from 0 to 9), and ends with a ")", so it will delete 5 characters, it will not necessarily be at the start of a name, another example

RandomName1(e25)RandomName2.txt
to
RandomName1RandomName2.txt

i hope it was clear enough, thanks for the guidance
mikado86
 
Posts: 5
Joined: Sun Mar 29, 2020 8:16 pm

Re: Simple renaming

Postby therube » Mon Mar 30, 2020 12:25 pm

1:RegEx
Code: Select all
Match:  (.*?)(\(e\d\d\))(.*)
Replace:  \1\3

Match anything, non-greedy
up to, paren+e+#+#+paren, \(e\d\d\)
followed by anything else.

Keep any matching first part
ignore the second part
keep any matching third part.
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Simple renaming

Postby RegexNinja » Mon Mar 30, 2020 12:29 pm

Hi.. With (e85)RandomName1, what are the variables to keep/remove?
Case1: Remove: 1st-5chars
Case2: Remove 1st5hars when its like: (Any3Chars)
Case3: Remove 1st5hars when its like: (eAny2Digits)
Case4: Remove 1st5hars when its like: (1LowerCaseAny2Digits)
Case5: Specify: RandomNameFormat (and lose everything else)


Here's a regex-replace to handle the Case4 condition, it seems the most likely case.
I'm just learning javascipt myself, so I dunno if its the best way, lol

newName=name.replace(/\([a-z]\d{2}\)(.*)/, '$1')
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Simple renaming

Postby RegexNinja » Mon Mar 30, 2020 12:33 pm

Woops! Nobody had replied when I started typing the post, lol..
This only matches the Case3 condition:

newName=name.replace(/\(e\d{2}\)(.*)/, '$1')
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Simple renaming

Postby RegexNinja » Mon Mar 30, 2020 12:55 pm

Sorry. Just noticed you want to remove ALL occurrences of: (eAny2Digits)

newName=name.replace(/\(e\d{2}\)/g, '')
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Simple renaming

Postby therube » Mon Mar 30, 2020 1:03 pm

(Oops on my end too. Didn't realize we were in JavaScript renaming.)
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm

Re: Simple renaming

Postby mikado86 » Mon Mar 30, 2020 8:04 pm

RegexNinja wrote:Sorry. Just noticed you want to remove ALL occurrences of: (eAny2Digits)


Yes, thats what i want to do, normally, there will only be "one" case of "(eAny2Digits)" at most per file, but thats the gist of it.

therube wrote:(Oops on my end too. Didn't realize we were in JavaScript renaming.)


Yeah that is correct, i have a general idea of how to do it with a regular match/replace input, but as i said i have to do this constantly and with more values than "(eXX)", as far i am aware, i can only save/load one single match replace values, -- so i want to make a javascript to automate the process somewhat, thanks!
mikado86
 
Posts: 5
Joined: Sun Mar 29, 2020 8:16 pm

Re: Simple renaming

Postby mikado86 » Mon Mar 30, 2020 8:21 pm

RegexNinja wrote:Sorry. Just noticed you want to remove ALL occurrences of: (eAny2Digits)

newName=name.replace(/\(e\d{2}\)/g, '')



This solved my question btw, thanks a lot!, now i just need to learn to make a sequence but i should be able to figure that myself
mikado86
 
Posts: 5
Joined: Sun Mar 29, 2020 8:16 pm

Re: Simple renaming

Postby exige24 » Tue Nov 16, 2021 8:25 pm

RegexNinja wrote:Hi.. With (e85)RandomName1, what are the variables to keep/remove?
Case1: Remove: [u]1st-5chars[/u]
Case2: Remove 1st5hars when its like: (Any3Chars)
Case3: Remove 1st5hars when its like: (eAny2Digits)
Case4: Remove 1st5hars when its like: (1LowerCaseAny2Digits)
Case5: Specify: RandomNameFormat (and lose everything else)


Here's a regex-replace to handle the Case4 condition, it seems the most likely case.
I'm just learning javascipt myself, so I dunno if its the best way, lol

newName=name.replace(/\([a-z]\d{2}\)(.*)/, '$1')



Hi, I have a bunch of files that require removing the first 5 characters, similar to this (0946 The Catcher and the Rye) and this is the perfect solution, but it was never addressed because the OP needed something different. Would you care to expand on how that could be done?
exige24
 
Posts: 1
Joined: Tue Nov 16, 2021 8:21 pm

Remove beginning "(digits " and the next ")"

Postby Luuk » Wed Nov 17, 2021 3:46 am

This can also be with RegEx(1) but would need to see examples like... (0946 The Catcher and the Rye).txt ====> The Catcher and the Rye.txt
The RegEx(1) would use a "Match" and "Replace" like...
^\(\d{4} (.+?)\)(.*)
\1\2
Luuk
 
Posts: 692
Joined: Fri Feb 21, 2020 10:58 pm

Re: Simple renaming - remove (eXX) from file name

Postby Thiago » Sat Feb 12, 2022 2:34 am

Hi! I have an equally complex case.
I work with software that generates an output file with the names: JOB_1_sample-file_20220211083122.txt.

The beginning of the text file is variable (JOB_1,JOB_2,JOB_3,etc).

Example: JOB_2_sample-file_20220211083122.txt, JOB_34_sample-file_20220211083122.txt, JOB_1007_sample-file_20220211083122.txt.

I need to remove the initials from the files, leaving only the 'sample' forward.

Is there such a possibility?
Thiago
 
Posts: 2
Joined: Sat Feb 12, 2022 2:30 am

Remove the leading "JOB_digits_"

Postby Luuk » Sat Feb 12, 2022 3:05 am

With RegEx(1) the "Match" and "Replace" can be like...
^JOB_\d+_(.+)
\1

With javascript, the code can be like...
newName=name.replace(/^JOB_\d+_(.+)/, '$1')
Luuk
 
Posts: 692
Joined: Fri Feb 21, 2020 10:58 pm

Next

Return to Javascript Renaming