by RegexNinja » Mon May 25, 2020 7:04 am
Hi guys, I've been doing some research, so maybe I can help clear a few things up.
There's nothing wrong with case-converting elements in an array, if those elements are text-strings.
All elements in an array created by split() are text-strings, even against names like 1234, so no problems there.
Problems arise if you apply methods like .toLowerCase against non-existent element-numbers in an array.
To illustrate, create names like: AnyName.txt, Any_Name.txt, then use:
a=name.split("_")
newName=a[0].toUpperCase()
You'll quickly see the 1st-element in the array for all files.. NO errors because a[0] always exists.
Now use a[3] instead..... Errors-a-plenty when .toUpperCase has no text-string to convert.
Removing .toUpperCase() removes the error. You can still reference non-existent element-numbers.
And you can even do comparisons against them like: if (a[4]===a[5]) {whatever}
But if they dont exist, you're comparing ('undefined'==='undefined') which evaluates to true.
You could eliminate errors by pre-checking for undefined like: if (typeof a[5] !== 'undefined') {More if's, etc}
But its a waste of code, and doesnt keep non-renamed files from consuming the counter's sequential numbering.
Since every file in BRU's right-pane, whether selected or not, will consume one of those numbers.
For instance, you could use this:
function test() { // ......................... Create a function called 'test' that when run, would:
a=name.split("_") // ............................ Set a = OrigName split into array (using _ as delimiter)
if (typeof a[4] !== 'undefined') { // ............ IF array has a 5th-element, then:
if (typeof a[5] !== 'undefined') { // ................ IF array has a 6th-element, then:
if (a[4].toLowerCase()===a[5].toLowerCase()) { // IF 5th-element(lowercased) = 6th-element(lowercased), then:
a[4]="AZ"+("0000"+counter).slice(-4) // ............... Replace 5th-element with AZ(0000+counter), cropped to Last4Chars
newName=a.join("_") // ................................. Set NewName = a's elements rejoined with _ as their separator
return newName}}}} // ................................... Process NewName
test(name) // ................................ Run the function test against the variable name
To get this:
AnyName ---------------------------------------------------> (not renamed, but throws counter off)
Beth_R_Mulford_11-09-1991_null_null_1996 ------------> Beth_R_Mulford_11-09-1991_AZ0002_null_1996
Eric_J_Smith_02-01-1983_null_null_2008 ---------------> Eric_J_Smith_02-01-1983_AZ0003_null_2008
John_q_Adams_01-01-1900_null_null_null --------------> John_q_Adams_01-01-1900_AZ0004_null_null
Nancy_null_Davies_09-01-1900_null_NULL_1963 -------> Nancy_null_Davies_09-01-1900_AZ0005_NULL_1963
Nancy_null_Davies_09-01-1900_null_NULLNO_1963 ---> (not renamed, but throws counter off)
Rick_null_Jones_02-01-1900_null_null_2000 ------------> Rick_null_Jones_02-01-1900_AZ0007_null_2000
So the numbering wont be sequential when you have non-renamed files showing up in BRU's right-pane.
The easy fix, for both the counter & verifying that all names in BRU'S right-pane have a 5th and 6th-element:
#12Mask with Regex=Checked:
^(?i)[^_]+?_[^_]+?_[^_]+?_[^_]+?_([^_]+?)_\1_
That guarantees at least 1-char (besides underscore) in the 5th/6th-elements, & keeps the numbering sequential.
With the 12Filter, you can just use this:
function test() { // ......................... Create a function called 'test' that when run, would:
a=name.split("_") // ................................... Set a = OrigName split into array (using _ as delimiter)
if (a[4].toLowerCase()===a[5].toLowerCase()) { // IF 5th-element(lowercased) = 6th-element(lowercased), then:
a[4]="AZ"+("0000"+counter).slice(-4) // ............... Replace a's 5th-element with AZ(0000+counter), cropped to Last4Chars
newName=a.join("_") // ................................. Set NewName = a's elements rejoined with _ as their separator
return newName}} // ..................................... Process NewName
test(name) // ................................ Run the function test against the variable name
To get this:
AnyName.txt ---------------------------------------------> filtered away (no toLowerCase-error, cant throw counter off)
Beth_R_Mulford_11-09-1991_null_null_1996-----------> Beth_R_Mulford_11-09-1991_AZ0001_null_1996
Eric_J_Smith_02-01-1983_null_null_2008--------------> Eric_J_Smith_02-01-1983_AZ0002_null_2008
John_q_Adams_01-01-1900_null_null_null--------------> John_q_Adams_01-01-1900_AZ0003_null_null
Nancy_null_Davies_09-01-1900_null_NULL_1963------> Nancy_null_Davies_09-01-1900_AZ0004_NULL_1963
Nancy_null_Davies_09-01-1900_null_NULLNO_1963 --> filtered away (cant throw counter off)
Rick_null_Jones_02-01-1900_null_null_2000-----------> Rick_null_Jones_02-01-1900_AZ0005_null_2000
Sorry the research took so long, I wanted to test everything before posting.
Hope I got it here in time for the manual.. If nothing else, it was a good learning experience.
Cheers!