Rename files by adding part of folder name

Bulk Rename Utility How-To's

Rename files by adding part of folder name

Postby MrMix » Thu Jul 10, 2025 4:34 pm

Hello everyone,

I have hundred of folders and thousand of files to be renamed, here below a simple example of the actual structure of folders and files

John
¦_____ John - 2000 - NewYork Winter
¦ ¦______________ 01 Mainstr.jpg
¦ ¦______________ 02 PlazaHotel.jpg
¦
¦_____ John - 2001 - Boston April
¦ ¦______________ 01 ParkAve.jpg
¦ ¦______________ 02 SpargoPizza.jpg
¦
¦_____ John - 2002 - Miami Late Summer
¦
Part 1
¦______________ 01. SouthBeach.jpg
¦______________ 02. lobby.jpg
¦______________ 03. roofview.jpg
¦
Part 2
¦______________ 01. NorthBeach.jpg
¦______________ 02. restaurant.jpg
¦______________ 03. BeachByNight.jpg

and so on

Here below how it should be after the bulk rename of the files:

John
¦_____ John - 2000 - NewYork Winter
¦ ¦______________ 01. NewYork Winter - Mainstr.jpg
¦ ¦______________ 02. NewYork Winter - PlazaHotel.jpg
¦
¦_____ John - 2001 - Boston April
¦ ¦______________ 01. Boston April - ParkAve.jpg
¦ ¦______________ 02. Boston April - SpargoPizza.jpg
¦
¦_____ John - 2002 - Miami Late Summer
¦
Part 1
¦______________ 01. Miami Late Summer - SouthBeach.jpg
¦______________ 02. Miami Late Summer - lobby.jpg
¦______________ 03. Miami Late Summer - roofview.jpg
¦
Part 2
¦______________ 01. Miami Late Summer - NorthBeach.jpg
¦______________ 02. Miami Late Summer - restaurant.jpg
¦______________ 03. Miami Late Summer - BeachByNight.jpg

I'd like to add the last part of the folder name to any files into that folder and sub folders

Thank you in advance for your help
MM
MrMix
 
Posts: 5
Joined: Thu Mar 09, 2023 5:55 pm

Re: Rename files by adding part of folder name

Postby Admin » Fri Jul 11, 2025 2:01 am

Hi, thank you for posting on the forum.
Is the part of the folder name to be added always after the last - character?
What is Part 1 and Part2 ? an extra subfolder?
Admin
Site Admin
 
Posts: 2981
Joined: Tue Mar 08, 2005 8:39 pm

Rename sub-files by adding parts of Folder in FolderPath

Postby Luuk » Fri Jul 11, 2025 5:59 am

Since its not posted in a format like... Folder1\Folder2\NowName ---> Folder1\Folder2\NewName
I'm also not understanding where these folder-depths are for the 'Part 1' and 'Part 2' folder-names??
Maybe there was supposed to be some leading-characters, that the forum software auto-deletes??

You can experiment with this javascript, but its making many assumptions that might not be correct!!
So Im including some comments for what the assumptions are, and also how it likes to rename...

// insert = AnyText after " - YYYY - " within any Folder in FolderPath (no files deeper than 2-levels from AnyText).
// Only match AnyText within paths like "\John\John - " or "\Frank\Frank - " (not paths like "\John\Frank - ).
insert = object('folder').replace(/(?=.*\\([^\\]+)\\\1 - ).* - ((19|20)\d\d) - ([^\\]+)\\(?!([^\\]+\\){2,}).*$/,'$4')

//If AnyText was found for insert, also store it within mtch (so that mtch can be used as a /regexp/).
if (!/\\/.test(insert)) {mtch = new RegExp(insert)

// Test that name starts with digits, followed by periods or spaces && Test that name doesnt already have AnyText.
// (With both of the name-conditions being true), Replace leading 'numbers ' ---> 'numbers. ' + AnyText + remaining filename
if (/^\d+[. ]+/.test(name)&&!mtch.test(name)) newName=name.replace(/^(\d+)[. ]+.+/,'$1. ')+insert+' - '+name.replace(/^\d+[. ]+(.+)/,'$1')}

Renaming like.....
\John\John - 2000 - AnyText1\01 Mainstr.jpg --------------> \John\John - 2000 - AnyText1\01. AnyText1 - Mainstr.jpg
\John\John - 2012 - AnyText2\02 Hotel.jpg ----------------> \John\John - 2012 - AnyText2\02. AnyText2 - Hotel.jpg
\John\John - 2016 - AnyText3\Part1\01. Beach.jpg -------> \John\John - 2016 - AnyText3\Part1\01. AnyText3 - Beach.jpg
\John\John - 2016 - AnyText4\Part1\01. AnyText4.jpg ---> not renamed (filename already has AnyText4)
\John\John - 2016 - AnyText5\Part1\a\01. Sky.jpg --------> not renamed (filename >2-levels below AnyText5)
\John\Jane - 2016 - AnyText6\03. roofview.jpg -----------> not renamed (because of \John\Jane)
Luuk
 
Posts: 820
Joined: Fri Feb 21, 2020 10:58 pm

Re: Rename files by adding part of folder name

Postby MrMix » Fri Jul 11, 2025 8:28 am

Thank you for yr. reply

Admin wrote:Is the part of the folder name to be added always after the last - character?

Yes, it is
Admin wrote:What is Part 1 and Part2 ? an extra subfolder?

Yes, sorry the schema did not came out as it should.
'John' is the main folder.
'John - 2000 - NewYork Winter', 'John - 2001 - Boston April', 'John - 2002 - Miami Late Summer' are subfolders of 'John'
'Part1' and 'Part2' are subfolders of '2002 - Miami Late Summer'

Thank you and regards
MM
MrMix
 
Posts: 5
Joined: Thu Mar 09, 2023 5:55 pm

Re: Rename sub-files by adding parts of Folder in FolderPath

Postby MrMix » Fri Jul 11, 2025 8:52 am

Thank you for yr reply.

Please my answer to the previous msg.

Sorry but I have no idea how to handle javascript but I believe your result should be almost what I'm expecting (see notes here below)

Luuk wrote:\John\John - 2000 - AnyText1\01 Mainstr.jpg --------------> \John\John - 2000 - AnyText1\01. AnyText1 - Mainstr.jpg
\John\John - 2012 - AnyText2\02 Hotel.jpg ----------------> \John\John - 2012 - AnyText2\02. AnyText2 - Hotel.jpg
\John\John - 2016 - AnyText3\Part1\01. Beach.jpg -------> \John\John - 2016 - AnyText3\Part1\01. AnyText3 - Beach.jpg

Correct

Luuk wrote:\John\John - 2016 - AnyText4\Part1\01. AnyText4.jpg ---> not renamed (filename already has AnyText4)

This should be \John\John - 2016 - AnyText4\Part1\01. AnyText4 - AnyText4.jpg

Luuk wrote:\John\John - 2016 - AnyText5\Part1\a\01. Sky.jpg --------> not renamed (filename >2-levels below AnyText5)

This should be \John\John - 2016 - AnyText5\Part1\a\01. AnyText5 - Sky.jpg

Luuk wrote:\John\Jane - 2016 - AnyText6\03. roofview.jpg -----------> not renamed (because of \John\Jane)

This should be \John\Jane - 2016 - AnyText6\03. AnyText6 - roofview.jpg

Thanks and regards
MM
MrMix
 
Posts: 5
Joined: Thu Mar 09, 2023 5:55 pm

Re: Rename files by adding part of folder name

Postby Admin » Fri Jul 11, 2025 9:30 am

Two different renames seem to be needed, depending if the part of folder name to append is the folder itself or one folder up.

To add to Luuk scripts, here is a script that appends the "containing folder" part (you can paste it into Javascript in BRU)

Code: Select all
// Example before: name = "01 Mainstr", container folder = "John - 2000 - NewYork Winter"
// Example after: newName = "01. NewYork Winter - Mainstr"
var folderName = object("container");
var parts = folderName.split(" - ");
var eventTitle = parts.length >= 3 ? parts[2] : folderName;
var match = name.match(/^(\d+)\s+(.*)$/);
if (match) {
    // prepend index with dot, insert event title, then original descriptor
    newName = match[1] + ". " + eventTitle + " - " + match[2];
} else {
    // if pattern doesn't match, leave name unchanged
    newName = name;
}


Here is another script for one folder up:

Code: Select all
// Example before: name = "01 Mainstr", folders = "John - 2000 - NewYork Winter\part1\"
// Example after: newName = "01. NewYork Winter - Mainstr"

// get the 2nd-level parent folder (e.g. "NewYork Winter")
var parent2 = object("container[2]");

// split off the numeric prefix and the rest of the name
var sep = name.indexOf(" ");
var numPart = sep >= 0 ? name.substring(0, sep) : name;
var restPart = sep >= 0 ? name.substring(sep + 1) : "";

// assemble the new name: "number. parent2 - rest"
newName = numPart + ". " + parent2 + (restPart ? " - " + restPart : "");


test them out, I used:

https://bulkrename.software/js/

cheers
Admin
Site Admin
 
Posts: 2981
Joined: Tue Mar 08, 2005 8:39 pm

Rename sub-files by adding parts of Folder in FolderPath

Postby Luuk » Fri Jul 11, 2025 11:24 am

It could be very dangerous to rename like you're saying, unless you're certain of all of the fullpathnames??
It probably safer to limit the recursion to 3-or-4 folder-depths, but of course you're more familiar with them.
After removing the safety features (recursion/pre-existing AnyText/repeating-name) it can be simplified to...

// insert = AnyText after " - YYYY - " within any Folder in FolderPath
insert = object('folder').replace(/.*\\[^\\]+ - ((19|20)\d\d) - ([^\\]+)\\.*$/, '$3')

//If AnyText was found,
if (!/\\/.test(insert)) {

// Test that name starts with digits, followed by periods or spaces, (but not a digit afterwards)
// (If the name-condition is true), then replace leading 'numbers ' --> 'numbers. ' + AnyText + remaining Filename
if (/^\d+[. ]+(?!\d)/.test(name)) newName=name.replace(/^(\d+)[. ]+.+/,'$1. ')+insert+' - '+name.replace(/^\d+[. ]+(.+)/,'$1')}


Now it renames without any of those safety-features, but Im added (?!\d) just trying to keep from splitting-up any dates.
If you dont have names starting with dates (or other numbers that should stay together), then its safe to remove (?!\d).
Remember, using this more-than-once just keeps re-inserting AnyText each time, with that safety-feature now removed!
Luuk
 
Posts: 820
Joined: Fri Feb 21, 2020 10:58 pm


Return to How-To


cron