Complex File Rename Using Javascript

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

Complex File Rename Using Javascript

Postby michael » Sat Apr 18, 2020 1:16 am

I am struggling to retain an incrementing count variable in part of a file name. Each time a file name is renamed my JavaScript is run again resetting my count variable. I can only change the fifth part of the filename and this part must be unique (more than 5000 files to be renamed)

This is a rather complex rename project so I've listed examples below. Any recommendations??

Before:
John_q_Adams_01-01-1900_null_null_null
Rick_null_Jones_02-01-1900_null_null_2000
Eric_J_Smith_02-01-1983_null_null_2008
Beth_R_Mulford_11-09-1991_null_null_1996
Nancy_null_Davies_09-01-1900_null_NULL_1963

After: (current outcome)
John_q_Adams_01-01-1900_AZ0001_null_null
Rick_null_Jones_02-01-1900_AZ0001_null_2000
Eric_J_Smith_02-01-1983_AZ0001_null_2008
Beth_R_Mulford_11-09-1991_AZ0001_null_1996
Nancy_null_Davies_09-01-1900_AZ0001_NULL_1963

After: (desired outcome, count correctly incrementing)
John_q_Adams_01-01-1900_AZ0001_null_null
Rick_null_Jones_02-01-1900_AZ0002_null_2000
Eric_J_Smith_02-01-1983_AZ0003_null_2008
Beth_R_Mulford_11-09-1991_AZ0004_null_1996
Nancy_null_Davies_09-01-1900_AZ0005_NULL_1963

current javascript code:
```
var str = name;

// global increment variable
var increment = (function (n) {
return function () {
n += 1;
return n;
};
})(0);

function test(str) {
// split string by '_' delimiter
a = str.split("_");
b = a[4];
c = a[5];
// compare fifth and sixth indexes of string
if (b === c) {
// if equal (e.g. null and null) replace fifth index with "AZ" and a unique four digit number with leading zeros
a[4] = "AZ" + ("0000" + increment()).slice(-4);
newName = a.join("_");
return newName;
} else {
newName = a.join("_");
return newName;
}
}

test(str);
```
michael
 
Posts: 6
Joined: Sat Apr 18, 2020 12:41 am

Re: Complex File Rename Using Javascript

Postby RegexNinja » Sat Apr 18, 2020 2:44 am

Unfortunately, you cant set global variables right now (ones that persist after a file rename).
So your variable keeps resetting after each file rename, I'm trying to learn javascript & this was a major stumbling block for me.
Please see: http://www.bulkrenameutility.co.uk/foru ... =11&t=4791

The easiest thing to do, is either use the global-variable: counter or object('autonumber') with the appropriate #10Settings
Basically, just kill the var increment as a function, & replace: ("0000" + increment()).slice(-4) with ("0000"+counter).slice(-4)
From the above post, it sound like they're gonna support more global variables in the future (fingers crossed).
Cheers.
RegexNinja
 
Posts: 135
Joined: Fri Feb 21, 2020 5:26 pm

Re: Complex File Rename Using Javascript

Postby Admin » Sat Apr 18, 2020 2:51 am

If you need global vars maybe try :

setEnv("varname", "value")
Sets the value of an environment variable
ex: setenv('BRUNAME', name);

getEnv("varname")
Gets the value of an environment variable
ex: var value = getenv('USERNAME');

When the global var counter is 1 you know the renaming operation just started for first name otherwise it is already the second or third etc.
Admin
Site Admin
 
Posts: 1846
Joined: Tue Mar 08, 2005 8:39 pm

Re: Complex File Rename Using Javascript

Postby michael » Sat Apr 18, 2020 3:16 am

Want to thank RegexNinja for the response!! Using the counter variable seems to have solved things for me. I've not tried the Admin's approach to using global environment, but that approach would have also fixed things for me. Really appreciate the help!!
michael
 
Posts: 6
Joined: Sat Apr 18, 2020 12:41 am

Re: Complex File Rename Using Javascript

Postby trm2 » Thu May 21, 2020 2:41 pm

Doesn't work on your last sample:

Nancy_null_Davies_09-01-1900_AZ0005_NULL_1963

Here is the revised code:



var str = name;


function test(str) {
// split string by '_' delimiter
a = str.split("_");
b = a[4];
c = a[5];
// compare fifth and sixth indexes of string
if (b === c) {
// if equal (e.g. null and null) replace fifth index with "AZ" and a
unique four digit number with leading zeros
a[4] = "AZ" + ("0000"+counter).slice(-4);
newName = a.join("_");
return newName;
} else {
newName = a.join("_");
return newName;
}
}
test(str);




Admin if you would please assist.
trm2
 
Posts: 165
Joined: Wed Jan 15, 2020 12:47 pm

Re: Complex File Rename Using Javascript

Postby michael » Sat May 23, 2020 7:15 pm

The file to test should be "Nancy_null_Davies_09-01-1900_null_NULL_1963" to achieve the result "Nancy_null_Davies_09-01-1900_AZ0005_NULL_1963".

Here is the final code I used, successfully renaming files based on my parameters. Hope it helps.

var str = name;

function test(str) {
a = str.split("_");
b = a[4];
c = a[5];
if (b === c) {
a[4] = "AZ" + ("00000" + (counter)).slice(-5);
newName = a.join("_");
return newName;
} else if (b.substr("-4", 4) === c) {
a[5] = "null";
newName = a.join("_");
return newName;
} else {
newName = a.join("_");
return newName;
}
}

test(str);
michael
 
Posts: 6
Joined: Sat Apr 18, 2020 12:41 am

Re: Complex File Rename Using Javascript

Postby trm2 » Sat May 23, 2020 9:37 pm

I pasted the code and tested - OK

Still did not rename the file,

Nancy_null_Davies_09-01-1900_null_NULL_1963

Verified the code again. checked to make sure the file name sample is the exact one it should be - check.

It does not like that uppercase NULL. Works for all other samples. By the way I didn't mean to confuse you - I included the 'expected' outcome in the previous post, but
that was not the sample I used. the one you quoted is the one I have been testing all along.

Need to make this work to include in book and soon.

Thanks.
trm2
 
Posts: 165
Joined: Wed Jan 15, 2020 12:47 pm

Re: Complex File Rename Using Javascript

Postby trm2 » Sat May 23, 2020 10:15 pm

I got it:

function test() {
a=name.split("_")
a[4]="AZ"+("00000"+counter).slice(-4)
newName=a.join("_")
return newName}
test(name);


Works for all samples.
trm2
 
Posts: 165
Joined: Wed Jan 15, 2020 12:47 pm

Re: Complex File Rename Using Javascript

Postby michael » Sat May 23, 2020 10:21 pm

You are correct. My apologies. Lets try changing the variable before comparison:

a = str.split("_");
b = a[4].toUpperCase();
c = a[5].toUpperCase();

Try this, just tested and it seems to work. Thank you for pointing out a way to make this better! I hope this tests well for you.

var str = name;

function test(str) {
a = str.split("_");
b = a[4].toUpperCase();
c = a[5].toUpperCase();
if (b === c) {
a[4] = "AZ" + ("00000" + (counter + 538)).slice(-5);
newName = a.join("_");
return newName;
} else if (b.substr("-4", 4) === c) {
a[5] = "null";
newName = a.join("_");
return newName;
} else {
newName = a.join("_");
return newName;
}
}

test(str);
michael
 
Posts: 6
Joined: Sat Apr 18, 2020 12:41 am

Re: Complex File Rename Using Javascript

Postby michael » Sat May 23, 2020 10:41 pm

***Edit* This should be correct***

var str = name;

function test(str) {
a = str.split("_");
b = a[4].toUpperCase();
c = a[5].toUpperCase();
if (b === c) {
a[4] = "AZ" + ("00000" + (counter)).slice(-5);
newName = a.join("_");
return newName;
} else if (b.substr("-4", 4) === c) {
a[5] = "null";
newName = a.join("_");
return newName;
} else {
newName = a.join("_");
return newName;
}
}

test(str);
michael
 
Posts: 6
Joined: Sat Apr 18, 2020 12:41 am

Re: Complex File Rename Using Javascript

Postby trm2 » Sat May 23, 2020 10:56 pm

You can't use .toUpperCase() with an array. It won't work - also failed test of course.

The only way is to separate the elements first then use the function and add them back in - a real painful roundabout way of doing it - don't ask me how - I just report the news I don't make it.
trm2
 
Posts: 165
Joined: Wed Jan 15, 2020 12:47 pm

Re: Complex File Rename Using Javascript

Postby michael » Sat May 23, 2020 11:30 pm

I’m sure you’re correct. Sorry I couldn’t help.
michael
 
Posts: 6
Joined: Sat Apr 18, 2020 12:41 am

Re: Complex File Rename Using Javascript

Postby trm2 » Sun May 24, 2020 2:27 am

That's Okay. The code I posted is working fine for all samples and that is what I'll use. Unless someone has anything better as
an alternative which I will also use in addition if it comes in time (always looking for alternative methods). I have been slowing down
as far as adding new material. Soon the door will be closed for any new admissions. Getting things ready. Not sure when but it is
certainly sooner than expected. After final draft is proofread and any errors addressed, I have to get the navigation system going.
That could take anywhere from a week or longer depending on my progress. Then it should be ready to publish.

I was hoping to have more JavaScript examples. Over all, there wasn't too much material, but I implemented what I
could. I saw where someone just posted another one - I will take a look at that.

It would be another year at least if I hadn't done a 'Back to The Future' - meaning I wrote this at the same time I was writing the first
volume. Very confusing at times hehe. By the way, if anyone is interested, the length of Tolstoy's War and Peace has nothing on this volume.
trm2
 
Posts: 165
Joined: Wed Jan 15, 2020 12:47 pm

Re: Complex File Rename Using Javascript

Postby 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!
RegexNinja
 
Posts: 135
Joined: Fri Feb 21, 2020 5:26 pm


Return to Javascript Renaming