Appending Parent Folder Elements into File Name

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

Appending Parent Folder Elements into File Name

Postby soundarchivist » Fri May 28, 2021 4:01 pm

I want to be able to rename files within a folder based off of elements of the parent folder’s name as well as adding in each file’s CurrentModifiedDate in YYYYMMDDHHMM and a file counter to create a unique identifier code for each file that’ll be extracted and embedded as metadata into the file with another piece of software. The variables in blue and green change depending upon the session, so they need to be referenced from the parent folder. The dashes and underscore are important elements to be included as well.

Currently, I accomplish this by running BRU several times while changing settings in-between runs and manually entering some information. I think it’s possible to truncate this all into one automated script, since my folder naming scheme is consistent, but my head starts spinning when I don’t have code to start with (I understand coding enough to modify existing code, but not enough to create much on my own).

I believe that this would fall under javascripting, since it involves referencing the directory. Please let me know if I should post this on a different part of the forum though. Example to follow:

Example Parent Folder: 2021.05.28_---_APN_788T_USA, Montana, Lewistown_Distant Gunfire, Urban

Original File Names:
Recording001.wav
Recording002.wav
Recording003.wav

Renaming Formula:
Recording001_FileCurrentModifiedDate(YYYYMMDDHHMM)-APN-788T-Counter###.wav

Final Renamed Files:
Recording001_202105280921-APN-788T-001.wav
Recording002_202105281005-APN-788T-002.wav
Recording003_202105282201-APN-788T-003.wav
soundarchivist
 
Posts: 7
Joined: Tue May 05, 2020 4:46 am

Re: Appending Parent Folder Elements into File Name

Postby therube » Fri May 28, 2021 5:09 pm

(Without dealing with the parent folder bit...)


7:Add -> Suffix: '_'

8:Auto Date
Mode: Suffix
Type: Modified Current
Fmt: Custom
Custom: %Y%m%d

10:Numbering
Mode: Suffix
Pad: 3
therube
 
Posts: 998
Joined: Mon Jan 18, 2016 6:23 pm

Re: Appending Parent Folder Elements into File Name

Postby therube » Fri May 28, 2021 5:12 pm

To note:

8:Auto Date, when Fmt: is Custom, Sep: is ignored.

I didn't really expect that.

So...

(In my steps above) you could ignore 7:Add, & change 8's:Auto Date, Custom: to '_%Y%m%d'.
therube
 
Posts: 998
Joined: Mon Jan 18, 2016 6:23 pm

Re: Appending Parent Folder Elements into File Name

Postby soundarchivist » Fri May 28, 2021 5:38 pm

Thanks for your response, TheRube.

Currently, I use:

8. Autodate)
Mode:Suffix
Type: Modified (New)
FMT: YMD HM
Sep: _

10. Numbering)
Suffix
Pad: 3
Sep: -APN-788T-

This creates the filename as I want, but requires me to go into the Sep field on 10 and manually change the "APN" and "788T". It'd really speed up my process if I could have it reference the parent folder which already contains that information, as those two bits of information can change a lot in the course of my work.
soundarchivist
 
Posts: 7
Joined: Tue May 05, 2020 4:46 am

Re: Appending Parent Folder Elements into File Name

Postby Luuk » Fri May 28, 2021 6:55 pm

Im not expert to know the javascript, so the best I can do is give links... http://www.bulkrenameutility.co.uk/foru ... 802#p12794
The example does match one part of the parent folder name, and also adds a suffix for the autonumber to the end.
Im trying to modify the code, but so far its not conducting properly, so Im hoping that you can decipher.

If finding a solution for the syntax, please do post the code, so helping other users to also learn the javascript.
With the exiftool, this is all very easy to conduct, but with javascript the syntax is very confusing for me.
If its not illegal, maybe also posting at the link, so to get better advice for modifying the code.

If asking for more help, it might help to say how the blue and green can change (unless its always between the underscores).
Another confusing part, is does the brown come from $Folder+$FileModifyTime or $Folder+$CurrentLocalTime or $FileModifyDate ?
Im not know how to conduct any of that anyway with the javascript, but thinking the experts to ask similiar questions.

But for the parent folder regex, Im thinking a match and replace like...
(\d{4})\.(\d\d)\.(\d\d)_---_([A-Z]{3})_(\d{3}[A-Z])_.+
'original-filename' + '_' + '$1$2$3' + '-' + 'unknown-time' + '-' + '$4' + '-' + '$5' + '-' + object("autonumber")
Except I know its not the right format, because Im already trying many similiar formats inside the javascript.

If you can discover the syntax, I can help with the regex, but you would have to convert the regex into javascript code.
You wont need to use '$1$2$3', if YYYYMMDD comes from $FileModifyDate (Im no idea what means $CurrentModifiedDate).
Its confusing because AutoDate(8) with Modified(New) never changes a filename, unless setting new timestamps with Special(14).
Luuk
 
Posts: 241
Joined: Fri Feb 21, 2020 10:58 pm

Re: Appending Parent Folder Elements into File Name

Postby soundarchivist » Fri May 28, 2021 7:49 pm

Luuk,

Thank you for looking into this; I appreciate it and will research the links you provided.

To clarify, the green and blue only change between the underscores. I use a lettercode to denote who was operating what machine. For the most part, Green is always 3 characters long. Whereas blue can change between 2-8 characters

So, there could be folders such as:
2021.08.28_---_APN_788T_Place_Subject Matter
2021.10.21_---_SMT_F8N_Place_Subject Matter
2021.12.13_---_HCM_SCORPIO_Place_Subject Matter

As for the brown information, that comes directly from Autodate(8) when it's set as a suffix with "Modified(Current)".
Luuk wrote:Its confusing because AutoDate(8) with Modified(New) never changes a filename, unless setting new timestamps with Special(14).

You can set (8) to change file names. Here's a screenshot to show that within BRU.

Image

As I said before, I'm not a programmer and am very ignorant of RegEx. Can what I'm trying to do be accomplished simply within RegEx? Do I even need to make Javascript?

Thanks,
soundarchivist
 
Posts: 7
Joined: Tue May 05, 2020 4:46 am

Re: Appending Parent Folder Elements into File Name

Postby soundarchivist » Fri May 28, 2021 7:52 pm

You'll to pardon me, as I mispoke when I said I used Modified(New). I meant to say that I used Modifed(Current)
soundarchivist
 
Posts: 7
Joined: Tue May 05, 2020 4:46 am

Re: Appending Parent Folder Elements into File Name

Postby Luuk » Sat May 29, 2021 2:03 pm

Its unfortunate, but RegEx(1) cannot see a file's parent folder, so never matching it. The only way to conduct this in one run, would be with the javascript code.
Without javascript, you would have to do something like first prefix the parent folder names, so then RegEx could have something to match on the next run.

The good news is that Im discovering the syntax for how to conduct regex inside the javascript with replace()
So the syntax is like... replace( /regex-match/ , replace1 + replace2 + replace3 + ... )
The bad news is I still have no ideas about how to conduct the dates, so this my best experiment so far...

In Numbering(10), the Mode must be "None", the Pad is "3", and must also have a checkmark in "Folde"

Inside Filters(12), to put a checkmark in "Subfolder", then for "Condition" to use this regex instead ...
/.*\\\d{4}\.\d\d\.\d\d_---_([A-Z]{2,4})_([\dA-Z]{2,8})_[^\\]+\\$/i.test(object('folder'))
Its just like the other page, except Im modified the regex, so if you need to experiment, must leave the red-parts alone!

The i makes /regex-match/ to be case-insensitive, so its safe to remove if you always need to match the capital letters.
But remember if changing the /regex/ in "Condition", to also change the same /regex/ inside of the javascript window.
So for the javascript window, this is the best Im getting so far ...
Code: Select all
newName=object('folder').replace(/.*\\\d{4}\.\d\d\.\d\d_---_([A-Z]{2,4})_([\dA-Z]{2,8})_[^\\]+\\$/i, name+'_'+object('modified').getTime()+'-$1-$2-'+object('autonumber'));
So its making the newnames like...
Fileame_1621394138000-APN-788T-001.wav
Fileame_1621457632700-HCM-SCORPIO-001.wav

This because object('modified').getTime() is making all the troubles, at first I tried without .getTime() but that was even worse!
I did also try things like .getTime("%Y%M%D") but the javascript does not seem to conduct date-elements like the perl or exiftool?
So now Im hoping maybe you can know some ways to better format the dates with some javascript code.

Im still going to look for better ways to conduct the javascript, because its really just too easy using the exiftool.exe with perl.
If you know any ways to format object('modified') with javascript, please to post them, because so far Im not finding any examples in the manual.
Luuk
 
Posts: 241
Joined: Fri Feb 21, 2020 10:58 pm

Re: Appending Parent Folder Elements into File Name

Postby soundarchivist » Sat May 29, 2021 2:32 pm

Thanks a lot, Luuk!

I removed the +'_'+object('modified').getTime()+ from the code and instead generated the Modified (Current) date from 8. Autodate and managed to get the correct results.

I used these settings

Autodate
Mode: Suffix
Type: Modified Current
Fmt: YMD HM
Sep: _

The file name is generating as it should.

I'd be curious to try to figure out how to draw the time out in Java, but for this use-case things are perfect!

Thank you so much!
soundarchivist
 
Posts: 7
Joined: Tue May 05, 2020 4:46 am

Re: Appending Parent Folder Elements into File Name

Postby Luuk » Sat May 29, 2021 5:38 pm

After much research, it seem that extracting the date-elements is a very common problem, because someone writes a whole library for it!
I did find many different element-codes to follow after object('modified') like .getFullYear(), .getMonth(), .getDate(), .getHours(), .getMinutes(), ...
But each .getPart is not properly formatted, like .getMonth presents "0" for January, so must always add 1, and then prefix a zero!

The only .get that was properly formatted was .getFullYear(), so all of the others would have to be reformatted, if to need leading zeroes.
So now Im understanding why everyone keeps saying download the 'moment' library, and sure enough it formats the date-elements much better.
The library is at https://momentjs.com and there is two versions to conduct exactly the same, but the big one also has comments for learning.

So after downloading the library, and naming it like 'moment.js', you can make it conduct the date-elements like this...
Code: Select all
require('C:/YourPath/To/moment.js');
newName=object("folder").replace(/.*\\\d{4}\.\d\d\.\d\d_---_([A-Z]{2,4})_([\dA-Z]{2,8})_[^\\]+\\$/i, name+'_'+moment(object("modified")).format('YYYYMMDDHHmm')+'-$1-$2-'+object("autonumber"));
.... ....

Just remember to use / instead of \ for the path to your moment.js or the bru will not find where the library is settled.
Im thinking this method a million times easier than trying to reformat every single date-element, so Im glad someone else goes to the trouble.
What I really like, is you dont even need the %signs for each element, and it also grants specifying different input-formats.
Luuk
 
Posts: 241
Joined: Fri Feb 21, 2020 10:58 pm


Return to Javascript Renaming