Page 1 of 1

NOOB asking for script help with two rename problems

Posted: 16 Jan 2020, 23:23
by Kallita
Thanks for any help provided.
I am a relative noob to programming but have been able to cobble together several scripts for the gui that run with relative correctness, but am having problems with 2 situations :
the first: I have a script to modify a file name but have some files that end with "(1)" "(2)" (3) etc [without the quotes] that usually indicate duplicates, and I can't figure out how to remove those brackets and number and not remove other brackets and contents. It is not on all files only those that are duplicate.
Second: I have a bunch of words (10 right now, but could add more) that I remove from the file name. Right now I am just using fn.removeAll("words") for each instance/word that i want to remove. some of the words start with a - and others do not. for example removeAll("-worda").removeAll("wordb").removeAll("word-c") . although this works I can't help thinking it is inefficient and was wondering if there was a way of doing it more succinctly that would allow for the future addition of more words.
thanks for your time and a great program
Kallita

Re: NOOB asking for script help with two rename problems

Posted: 17 Jan 2020, 15:48
by rednoah
1.
FileBot has a helper function for that one:

Code: Select all

"Test (1)".replaceTrailingBrackets() # Test

2.
/regular expressions/ allow you to describe generic text patterns succinctly: https://regexr.com/

Code: Select all

"A B C 1 2 3 4 5".removeAll(/B|C|[0-9]+/) # A

Re: NOOB asking for script help with two rename problems

Posted: 17 Jan 2020, 22:38
by Kallita
Thanks for the help!
I am using filebot on a windows 10 (if that matters) in plain file rename mode

here is an example of the file names i am running through filebot:
All Star Western 097 (1957) (c2c) (Superscan)
2000AD 2164 (2020) (Digital-Empire)(1)

here is the code I am using pretty simple

Code: Select all

d:\comics alphabetical/{n[0]}/{fn.removeIllegalCharacters().replaceAll(/[!?.]+$/).replaceAll(/[`´‘’ʻ_,.]/, " ").replaceFirst(' 0', '_0').replaceFirst(' 1', '_1').replaceFirst(' 2', '_2').replaceFirst(' 3', '_3').replaceFirst(' 4', '_4').replaceFirst(' 5', '_5').replaceFirst(' 6', '_6').replaceFirst(' 7', '_7').replaceFirst(' 8', '_8').replaceFirst(' 9', '_9').replaceFirst('_', "/")}
When using replaceTrailingBrackets() in this code it will remove the (1) <which is what I want >But it will also remove the (Superscan) from the first one<which I don't want>
I went to the regexr site that you had tagged in your reply and came up with a removeAll(\(1\)|\(2\)) that seemed to work on finding the characters but when I attempted to add it to the filebot script it gives me an unexpected \ error and won't save.
as I said I am a noob for programming so I am thinking I am just missing something. So any help is appreciated.
ps : not sure how to get the code box to pop up.... sorry
kallita

Re: NOOB asking for script help with two rename problems

Posted: 18 Jan 2020, 06:52
by kim

Code: Select all

.replaceFirst(' 0', '_0').replaceFirst(' 1', '_1').replaceFirst(' 2', '_2').replaceFirst(' 3', '_3').replaceFirst(' 4', '_4').replaceFirst(' 5', '_5').replaceFirst(' 6', '_6').replaceFirst(' 7', '_7').replaceFirst(' 8', '_8').replaceFirst(' 9', '_9')
=

Code: Select all

.replaceFirst(/\s(\d)/, '_$1')
remove e.g. (1) only at the end

Code: Select all

.replaceAll(/\(\d\)$/)

Re: NOOB asking for script help with two rename problems

Posted: 18 Jan 2020, 18:43
by Kallita
Thanks very much for the help Kim.
Both seem to be working perfectly.
I will continue to play with these and the whole program and hopefully learn more about the regex system.
Kallita

Re: NOOB asking for script help with two rename problems

Posted: 18 Jan 2020, 20:25
by rednoah
This might be too technical, but here's the official docs on slash strings:
https://docs.groovy-lang.org/latest/htm ... shy_string

Re: NOOB asking for script help with two rename problems

Posted: 19 Jan 2020, 19:22
by Kallita
thanks for the Groovy page. I will be going through it over the next little while.
kallita