Adding metadata tags based on different parts of the file name

Hello,

I have seen (and asked for help on) similar topics before and also tried to figure out how to do this myself, but I guess I'm just too stupid :stuck_out_tongue:

(Recently I have started playing around with generating images with AI locally using a software called NMKD. This software creates image files where the file name contains information on the settings used for creating it).

What I would like is to have a button that takes the information in the file names of the selected files and adds it as metadata tags, like this:

File name example:
1-2376659996-scale4.50-k_euler-sd-v1-5-fp16.png

The settings included in the file name:
1 = file number (not important)
2376659996 = Seed
scale4.50 = PromtGuidance
k_euler = Sampler
sd-v1-5-fp16 = Model

All the different settings are separated by a hyphen in the file names, except for the Model that also can include hyphens.

These are the metadata-tags I would like to be created from the file name:
Seed:2376659996
PromtGuidance:4.50
Sampler:k_euler
Model:sd-v1-5-fp16

A big thanks in advance!

Cheers!
/Samuel

Try

// https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122

// 2022-12-12

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var tmp = item.name_stem;

        var tag1 = tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, '$1');
        var tag2 = 'Seed:' + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, '$2');
        var tag3 = 'PromtGuidance:' + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, '$3');
        var tag4 = 'Sampler:' + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, '$4');
        var tag5 = 'Model:' + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, '$5');

        cmd.RunCommand('SetAttr FILE="' + item + '" META "tags:' + tag2 + ';' + tag3 + ';' + tag4 + ';' + tag5 + '"');
    }
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="label" textcol="none">
	<label>43122</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122</instruction>
		<instruction />
		<instruction>// 2022-12-12</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        var tmp = item.name_stem;</instruction>
		<instruction />
		<instruction>        var tag1 = tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, &apos;$1&apos;);</instruction>
		<instruction>        var tag2 = &apos;Seed:&apos; + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, &apos;$2&apos;);</instruction>
		<instruction>        var tag3 = &apos;PromtGuidance:&apos; + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, &apos;$3&apos;);</instruction>
		<instruction>        var tag4 = &apos;Sampler:&apos; + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, &apos;$4&apos;);</instruction>
		<instruction>        var tag5 = &apos;Model:&apos; + tmp.replace(/(.*?)-(.*?)-(.*?)-(.*?)-(.*)/, &apos;$5&apos;);</instruction>
		<instruction />
		<instruction>        cmd.RunCommand(&apos;SetAttr FILE=&quot;&apos; + item + &apos;&quot; META &quot;tags:&apos; + tag2 + &apos;;&apos; + tag3 + &apos;;&apos; + tag4 + &apos;;&apos; + tag5 + &apos;&quot;&apos;);</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>

https://resource.dopus.com/t/how-to-use-buttons-and-scripts-from-this-forum/3546

5 Likes

Thank you so much, Alexander

It worked like a charm :slight_smile:

Best regards,
Samuel

This is super helpful for anyone using custom RegEx columns as well. Allows you to have a copy of the metadata and ensures compatibility with certain applications that only look at the built-in meta-data fields without having to manual copy it all in.

1 Like

this will be very useful; Thanks lxp

Thank you.
I have been meaning to figure out how to add the folder name as a tag, but haven't tried yet.
I suppose this script could be modified to do that?

I have used folder names different parts as metadata in my flac files.

No need for a script:

@nodeselect
@sync:dopusrt /cmd SetAttr FILE={filepath} META "tags:{filepath|..|noterm|nopath}"
2 Likes

Thank you. However, it overwrites any existing Tags. I hope appending Tags is possible.

It is.

@nodeselect
@sync:dopusrt /cmd SetAttr FILE={filepath} META "tags:+{filepath|..|noterm|nopath}"

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Keywords_for_SetAttr_META.htm

1 Like

dear lxp this code doesn't works in my test.
I have been test this code on a flac file.

@nodeselect
@sync:dopusrt /cmd SetAttr FILE={filepath} META "albumartist:+{filepath|..|noterm|nopath}"

This code also overwrites any existing tags. its just add a plus sign before the folder name. so the plus sign doesn't works like a operator.

Only tags supports multiple entries that can be individually added resp. removed.

I am sorry I don't understand what does mean by tags? ‌‌albumartist is not a tags?

Opus uses the keyword tags to refer to a metadata tag named "tags" and the keyword albumartist to refer to a metadata tag named "albumartist".

Have a look at the link I posted above.