Rename files by adding first 4 Characters +

Bulk Rename Utility How-To's

Rename files by adding first 4 Characters +

Postby ctaveras@bpbcpa.com » Wed Jul 23, 2025 10:09 pm

I have about 500 + user folders, 557K files that need to be renamed in the following fashion just not sure how to go about this. I havent used BRU in over 5 years and staring at the interface is like a deer in headlights.


Here is my test folder Structure

0640 - John Smith (root folder)
--------------PERSONNEL FILE (Sub folder)
---------------------Folder 1 (Sub folder of PERSONNEL FILE)
------------------------File 1.PDF
------------------------File 2.XLS
------------------------File 3.DOC
---------------------Folder 2 (Sub folder of PERSONNEL FILE)
------------------------File 1.PDF
------------------------File 2.XLS
------------------------File 3.DOC

I need to do the following. For every file located in PERSONNEL FILE and its sub folders add to the file name PAYROLL_AND_BENEFITS, if the sub folder is EMPLOYMENT ELIGIBILITY the file should include EMPLOYMENT_AGREEMENT in the file name and so on for every other subfolder.

The file name should look like this, every file should include the first 4 characters of the root folder which is the employee ID and if the sub folder is PERSONNEL FILE add to the filename PAYROLL_AND_BENEFITS.

Final result

0730-PAYROLL_AND_BENEFITS-file 1.PDF
0730-PAYROLL_AND_BENEFITS-file 2.xls
0730-PAYROLL_AND_BENEFITS-file 3.DOC

Id appreciate any help.
ctaveras@bpbcpa.com
 
Posts: 7
Joined: Tue May 24, 2022 5:29 pm

Re: Rename files by adding first 4 Characters +

Postby Admin » Thu Jul 24, 2025 4:34 am

When do you add EMPLOYMENT_AGREEMENT as opposed to PAYROLL_AND_BENEFITS?
Admin
Site Admin
 
Posts: 3045
Joined: Tue Mar 08, 2005 8:39 pm

Rename files with part of folder-path

Postby Luuk » Thu Jul 24, 2025 8:17 am

IF the examples should be like.....
0640 - Name1\PERSONNEL FILE\File1 ------------------> 0640-PAYROLL_AND_BENEFITS-File1
0640 - Name1\PERSONNEL FILE\1\2\File2 -------------> 0640-PAYROLL_AND_BENEFITS-File2
0640 - Name1\PERSONNEL FILE\1\2\3\4\File3 --------> 0640-PAYROLL_AND_BENEFITS-File3
0730 - Name2\EMPLOYMENT ELIGIBILITY\File1 -------> 0730-EMPLOYMENT_AGREEMENT-File1
0730 - Name2\EMPLOYMENT ELIGIBILITY\1\File2 -----> 0730-EMPLOYMENT_AGREEMENT-File2
0730 - Name2\EMPLOYMENT ELIGIBILITY\1\2\File3 ---> 0730-EMPLOYMENT_AGREEMENT-File3

Then can try some javascript like...

if (object('isdir') == false) {
if (object('folder').match(/\\\d{4} - [^\\]+\\employment eligibility\\/i) && !name.match(/employment_agreement/i)) {
newName = object('folder').replace(/.*\\(\d{4}) [^\\]+\\employ.*/i, '$1-EMPLOYMENT_AGREEMENT-') + name }

if (object('folder').match(/\\\d{4} - [^\\]+\\personnel file\\/i) && !name.match(/payroll_and_benefits/i)) {
newName = object('folder').replace(/.*\\(\d{4}) [^\\]+\\personn.*/i, '$1-PAYROLL_AND_BENEFITS-') + name }}


This code is probably over-complicated, to keep from renaming any files that might have already been renamed.
I will post something better that's easier to understand later, but this might not be what you're wanting anyways.
Especially if the employeee id's should be increased by 90, or should also not be recursing so deeply?
Luuk
 
Posts: 833
Joined: Fri Feb 21, 2020 10:58 pm

Re: Rename files by adding first 4 Characters +

Postby ctaveras@bpbcpa.com » Mon Jul 28, 2025 4:35 pm

SO I have 3 sub folders under my main root folder

0630 - John SMith\Personnel File

All files in every sub folder under Personnel File should be named 0630-PAYROLL_AND_BENEFITS-filename123.doc

0630 - John SMith\Employment Eligibility

All files in every sub folder under Employment Eligibility should be named 0630- EMPLOYMENT_AGREEMENT-filename123.pdf

0630 - John SMith\Medical Records.

All files in every sub folder under Medical Records should be named 0630- Medical-filename123.xls
ctaveras@bpbcpa.com
 
Posts: 7
Joined: Tue May 24, 2022 5:29 pm

Re: Rename files by adding first 4 Characters +

Postby ctaveras@bpbcpa.com » Mon Jul 28, 2025 5:01 pm

Using a Java script is very different than that last time I used this utility. Let me add your script to the Java Section in BRU and see what I get.
ctaveras@bpbcpa.com
 
Posts: 7
Joined: Tue May 24, 2022 5:29 pm

Re: Rename files by adding first 4 Characters +

Postby ctaveras@bpbcpa.com » Mon Jul 28, 2025 6:40 pm

The Java script seems to be working and giving me the filenames that I need.

I know next to nothing about java script but i was able to following your syntax and created a new string to target files in the medical record subfolder.

if (object('folder').match(/\\\d{4} - [^\\]+\\medical records\\/i) && !name.match(/medical/i)) {
newName = object('folder').replace(/.*\\(\d{4}) [^\\]+\\medi.*/i, '$1-MEDICAL-') + name }}

However I am unsure how to target files in these subfolders

0630 - Smith, John\2023 and Forward\Medical Records
This should follow the same script you created

0630 - Smith, John\2023 and Forward\Personnel

Same for this one.

0630 - Smith, John\2023 and Forward\I-9s
This one should have Tax Form in the file name

0630 - Smith, John\2023 and Forward\Investigations

This one should have Investigations in the file name

I am just not sure how to target those files that are 2 folders deep in the structure.
ctaveras@bpbcpa.com
 
Posts: 7
Joined: Tue May 24, 2022 5:29 pm

Re: Rename files by adding first 4 Characters +

Postby ctaveras@bpbcpa.com » Thu Jul 31, 2025 11:45 pm

Hey there just checking to see if you had an idea on how to target the same types of files but in this structure

0630 - John SMith
--------->2023 and FOrward
------------->Personnel
------------------File 1.txt
------------------File 2.doc
------------->Investigations
------------->I-9s
-------------> Medical Records

Any files in any subfolder in Personnel should be rename 0630-personnel-FILE NAME.txt
Any files in I-9s and investigation should also be rename as such with 0630-personnel.file name.doc


I tried modifying your original script to target those files but its not working, any help would be greatly appreciated.

if (object('folder').match(/\\\d{4} - [^\\]+\\2023 and Forward\\/i) && !name.match(/medical/i)) {
newName = object('folder').replace(/.*\\(\d{4}) [^\\]+\\2023.*/i, '$1-MEDICAL-') + name }
ctaveras@bpbcpa.com
 
Posts: 7
Joined: Tue May 24, 2022 5:29 pm

Rename files with part of folder-path

Postby Luuk » Sun Aug 03, 2025 2:58 am

Its unfortunate but I wasnt able to answer, ever since the forums started requiring the javascript.
So Im just using someone else's browser, that does always conduct the javascript for any web-pages.

At first, when it was only 2-SubfolderNames, Im just using 2 if-statements, trying to make everything look easier.
But now with 5, its probably better to make 1-regex for all-5, then store parts of the matched-path into variables.
This way, if you get more subfolders in the future, the below format should be a little easier to modify...

// Create 1-regex to match certain folderpath-formats like below, so that all subfiles below there, should be targeted for renaming...
// Folder1 starts like "#### - ", then 1-allowed recursion-folder starting like "20## ", then any of your allowed SubfolderNames.
// If your recursion-folders dont always start with '20## ', just remove '20\d\d ' to allow 1-possible recursion-folder of any name.
// If needing more recursion to find your allowed SubfolderNames, can edit the "1" in {0,1} to set their maximum-depth below Folder1.
// Remember, its still only matching your folderpath-formats, all subfiles below allowed SubfolderNames still get targeted for renaming.
// To limit file-recursion below allowed SubFolderNames, can insert ($|([^\\]+\\){0,2}$) just before /i, editing 2 for your max file-recursion).
myFormats = new RegExp(/\\(\d{4}) - [^\\]+\\(?:20\d\d [^\\]+\\){0,1}(employment eligibility|personnel file|i-9s|investigations|medical records)\\/i)

// if (folderpath matches "\(4-digits) - Name\(?:AllowedRecursion)(Allowed SubFolderNames)\" case-insensitive AND is not a folder)...
if (object('folder').match(myFormats) && object('isdir') == false) {

// If found, store both matched-parts of folderpath (lower-cased) into path, remembering that .match() stores them as an [array]!
// This is why we used (grouping) inside of myFormats, so that with an array, path[1]=id# and path[2]=SubFolderName.
path = object('folder').toLowerCase().match(myFormats)

// Depending on the 5-SubfolderNames matched by path[2], create insert to store some insertion-text
if (path[2].startsWith('e')) insert = 'EMPLOYMENT_AGREEMENT'
if (path[2].startsWith('p')) insert = 'PAYROLL_AND_BENEFITS'
if (path[2].startsWith('m')) insert = 'MEDICAL'
if (path[2].startsWith('i-')) insert = 'TAX_FORM'
if (path[2].startsWith('in')) insert = 'INVESTIGATIONS'

// But what if the filename has already been inserted, and should not be renamed again with this insertion-text?
// We lowercase both now-name and insert for case-insensitive comparison, then only rename if insertion-text is missing...
// If now-name is not matched by insertion-text, then create the new-name like...
if (!name.toLowerCase().match(insert.toLowerCase())) newName = path[1] + '-' + insert + '-' + name}


It looks like your last-post decided the insertion-text might need to change, so this format should be easier to modify.
Luuk
 
Posts: 833
Joined: Fri Feb 21, 2020 10:58 pm

Re: Rename files by adding first 4 Characters +

Postby ctaveras@bpbcpa.com » Mon Aug 04, 2025 5:55 pm

Let me give that a go, and again thank you very much for taking the time!!
ctaveras@bpbcpa.com
 
Posts: 7
Joined: Tue May 24, 2022 5:29 pm

Re: Rename files by adding first 4 Characters +

Postby ctaveras@bpbcpa.com » Tue Aug 19, 2025 7:39 pm

Just wanted to follow up and say thank you for all your help. I had a coworker who knew Java and wrote the following which worked like a charm. Wanted to post it here in case others had a similar task.

// Dynamic rename (any depth after client) based on folder keywords
// Uses your mapping exactly as given

var folder = object('folder') || "";
var base = name || "";

// Normalize slashes, split path
var P = folder.replace(/\//g, "\\");
var segs = P.split("\\").filter(Boolean);

// Find "#### - ..." client folder index + id
var clientIdx = -1, id = null;
for (var i = segs.length - 1; i >= 0; i--) {
var m = segs[i].match(/^\s*(.{4,5})\s*-\s*/);
if (m) { clientIdx = i; id = m[1]; break; }
}
if (clientIdx < 0 || !id) { /* no match */ }
else {
// Segments under the client folder
var subs = segs.slice(clientIdx + 1).map(function (s) {
return s.toLowerCase().replace(/\s+/g, " ").trim();
});

// Mapping from keyword ? tag
var tagMap = {
"employment eligibility": "EMPLOYMENT-ELIGIBILITY",
"personnel": "PERSONNEL",
"personnel file": "PERSONNEL",
"i-9s": "PERSONNEL",
"investigations": "PERSONNEL",
"medical records": "MEDICAL",
"2023 and forward": "MEDICAL-RECORDS"
"payroll": "PAYROLL_AND_BENEFITS"

};
// Determine which keyword appears first in the path
var foundKey = null;
for (var k in tagMap) {
for (var j = 0; j < subs.length; j++) {
// Match exact keyword segment
if (subs[j] === k) {
foundKey = k;
break;
}
}
if (foundKey) break;
}

if (foundKey) {
var tag = tagMap[foundKey];

// Skip special case: "2023 and Forward" with 2023 already in filename
if (foundKey === "2023 and forward" && /2023/i.test(base)) {
// do nothing
} else {
var prefix = id + "-" + tag + "-";
if (!new RegExp("^" + id + "-" + tag + "-", "i").test(base)) {
newName = prefix + base;
}
}
}
}
ctaveras@bpbcpa.com
 
Posts: 7
Joined: Tue May 24, 2022 5:29 pm

Re: Rename files by adding first 4 Characters +

Postby Admin » Wed Aug 20, 2025 3:51 am

That's great 8)
Admin
Site Admin
 
Posts: 3045
Joined: Tue Mar 08, 2005 8:39 pm


Return to How-To