#1 : 27/04-20 23:08 dinesh
Posts: 4
|
File renaming with script method regex calculation within filename string
i m looking for guidance on problem renaming files i have got hundreds of filenames look like aa-95865-00-bb.ext aa-45732-00-bb.ext aa-85855-00-bb.ext aa-75263-00-bb.ext aa-35165-00-bb.ext i want to rename by replacing "00" with sum of five digits starting at index 4 eg "00" in the first filename to be replaced by 9+5+8+6+5 = 33, to be renamed as, aa-95865-33-bb.ext "00" in the second filename to be replaced by 4+5+7+3+2 = 21, to be renamed as, aa-45732-21-bb.ext and so on .. please guide .. thanks .. |
#2 : 28/04-20 01:04 David Lee
Posts: 1125
|
Try this:
match = item.name.match(/(.*-)(\d{5}-)00(.*)/); sum = 0; for (j = 0; j < 5; ++j) { sum += parseInt(match[2].charAt(j)); } return match[1] + match[2] + sum + match[3]; |
#3 : 29/04-20 01:47 dinesh
Posts: 4
|
Reply to #2:
thanks a lot, David Lee sir, your suggestion seems perfect i tried it, in script method got error (124) invalid script please guide further, whenever you have time thanks again |
#4 : 29/04-20 04:41 vevan
Posts: 2
|
Reply to #3:
var regex = item.name.match(/^(.*?)(\d{5}).*?(-.{2}$)/) var arr = (regex[2]).split('') var sum = arr.reduce(function(acc,cur){return acc*1+cur*1}) return regex[1] + regex[2] +'-'+ sum + regex[3] |
#5 : 29/04-20 09:16 David Lee
Posts: 1125
|
Reply to #3:
Dinesh My script works for me - check that you have entered it correctly. |
#6 : 29/04-20 09:20 David Lee
Posts: 1125
|
Reply to #4:
Vevan Neat trick! I wasn't aware of the Reduce method and Split on a null. Reduce is a bit more tricky to understand though and it took me a while to sort out the syntax. |
#7 : 30/04-20 11:33 vevan
Posts: 2
|
Reply to #6:
if ARen can support ES6, script will be more easy, just like this: var regex = item.name.match(/^(.*?)(\d{5}).*?(-.{2}$)/) var sum = Array.from(regex[2]).reduce((acc,cur)=>acc*1+cur*1) return regex[1] + regex[2] +'-'+ sum + regex[3] |
#8 : 30/04-20 18:07 David Lee
Posts: 1125
|
Reply to #7:
It doesn't! Neither "Array.from()" nor "=>" are supported by Aren |
#9 : 01/05-20 23:33 dinesh
Posts: 4
|
Reply to #5:
Thank you very much David Lee sir, yes, i retried it several times, and succeed actually, there were some files containing extra characters resulted in script error i also had to modify your code slightly to make it work for me, now it has worked, there is another complex renaming situation, i am trying to sort it myself will sincerely need your help if i fail thanks again.. |
#10 : 01/05-20 23:44 dinesh
Posts: 4
|
Reply to #4:
Thank you very much Vevan sir, i applied your code too, and it worked for me.. as David Lee said, its another wonderful approach to this situation i m grateful to both of you thanks again.. |