Is it possible to rename indefinite number of leading underscores with exclamation marks

Advanced Renamer forum
#1 : 06/09-12 18:29
Glen
Glen
Posts: 2
Hi!
Is it possible to rename leading underscores with exclamation marks in all files (file names) in directory, several directories?
Examle:
_____12pe4ort_349kkfl_qpe.txt --> !!!!!12pe4ort_349kkfl_qpe.txt
____qoepslf_[rotlh_cmbv.x.txt ---> !!!!qoepslf_[rotlh_cmbv.x.txt
___xzmqtit_qywor(oqoq).txt ---> !!!xzmqtit_qywor(oqoq).txt
__aldvm_qpw_xm_.txt --> !!aldvm_qpw_xm_.txt
Thank you so much!


06/09-12 18:29
#2 : 06/09-12 22:13
Stefan
Stefan
Posts: 274
Reply to #1:

FROM:
_____12pe4ort_349kkfl_qpe.txt
____qoepslf_[rotlh_cmbv.x.txt
___xzmqtit_qywor(oqoq).txt
__aldvm_qpw_xm_.txt


TO:
!!!!!12pe4ort_349kkfl_qpe.txt
!!!!qoepslf_[rotlh_cmbv.x.txt
!!!xzmqtit_qywor(oqoq).txt
!!aldvm_qpw_xm_.txt

TRY
the new scripting function with ARen 3.50 or newer.
Paste the code into an Script Method:
//--------Code Start
SubPattern = item.name.match(/^(_+)(.+)/);
exclamatio = SubPattern[1].replace(/_/g,'!');
return exclamatio + SubPattern[2] + item.ext;
//--------Code End


===EDIT:
but now i see ARen will not use this script:
"Ungueltiges Skript: line 3: SubPattern has no properties"

Ok, then for ARen we do it this way:

//--------Code Start
SubPattern = item.name.match(/^(_+)(.+)/);
exclamatio = SubPattern[1];
exclamatio = exclamatio.replace(/_/g,'!');
return exclamatio + SubPattern[2] + item.ext;
//--------Code End


Fine. Now after that renaming, the original code works too:

SubPattern = item.name.match(/^(_+)(.+)/);
exclamatio = SubPattern[1].replace(/_/g,'!');
return exclamatio + SubPattern[2] + item.ext;
=== /EDIT



Explanation:
"item.name" is a ARen var for each current file name processed.
"SubPattern" = Match one-or-more of leading underscores, and then the rest into an array.
"Exclamation" = Replace each matched underscore by an exclamation mark.
"return" = New filename will be exclamation marks and the original rest.
"item.ext" = the original file extension.


- --

But shouldn't an array start with zero?
Not that it is not nice for average users... but just confused (i am not an coder)

.


06/09-12 22:13 - edited 07/09-12 07:15
#3 : 07/09-12 09:21
Kim Jensen
Kim Jensen
Administrator
Posts: 880
Reply to #2:
Well spotted Stefan! One would think a simple task as this would be possible with the regular methods, but a script is unfortunately necessary.
You are right that the array is zero indexed. On index 0 you will find the complete match, which is basically everything.

This script will also work:
var match = item.newName.match(/[^_]/);
return new Array(match.index+1).join("!") + item.newName.substr(match.index);

The match.index contains the first index in the string that is not _. The newArray() part will create a string of ! in the length of the amount of _ in the beginning of the filename.
Because this script use item.newName it does not need to worry about the extension.

Both your and my solution works.

Relevant documentation can be found here:
http://developer.mozilla.org/en-US/docs/JavaScr ipt/Reference/Global_Objects/String/matc h
http://developer.mozilla.org/en-US/docs/JavaScr ipt/Reference/Global_Objects/String/repl ace
http://developer.mozilla.org/en-US/docs/JavaScr ipt/Guide/Regular_Expressions


07/09-12 09:21 - edited 07/09-12 09:22
#4 : 07/09-12 16:58
Glen
Glen
Posts: 2
Reply to #3:
Thanks Kim Jenson!
Thanks Stefan!

Everything is ok! Great! I am not JavaScript experienced user...
Thanks again!


07/09-12 16:58