# Adding metadata tags based on different parts of the file name

**URL:** https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122
**Category:** Help & Support
**Tags:** script
**Created:** [December 12, 2022, 2:33pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122 "2022-12-12T14:33:05Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![samuellidstrom](https://resource.dopus.com/user_avatar/resource.dopus.com/samuellidstrom/32/43592_2.png) [@samuellidstrom](https://resource.dopus.com/u/samuellidstrom)
#### Post date: [December 12, 2022, 2:33pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/1 "2022-12-12T14:33:05Z")

</div>

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 😛

(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

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [December 12, 2022, 5:11pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/2 "2022-12-12T17:11:52Z")

</div>

Try

```javascript
// 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**
>
> ```nohighlight
> <?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](https://resource.dopus.com/t/how-to-use-buttons-and-scripts-from-this-forum/3546)

---

<div class="post-metadata">

### Author: ![samuellidstrom](https://resource.dopus.com/user_avatar/resource.dopus.com/samuellidstrom/32/43592_2.png) [@samuellidstrom](https://resource.dopus.com/u/samuellidstrom)
#### Post date: [December 12, 2022, 8:39pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/3 "2022-12-12T20:39:08Z")

</div>

Thank you so much, Alexander

It worked like a charm 🙂

Best regards,  
Samuel

---

<div class="post-metadata">

### Author: ![evernessince](https://resource.dopus.com/letter_avatar_proxy/v4/letter/e/3ab097/32.png) [@evernessince](https://resource.dopus.com/u/evernessince)
#### Post date: [December 13, 2022, 7:48am UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/4 "2022-12-13T07:48:16Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![khalidhosain](https://resource.dopus.com/user_avatar/resource.dopus.com/khalidhosain/32/28729_2.png) [@khalidhosain](https://resource.dopus.com/u/khalidhosain)
#### Post date: [June 2, 2023, 8:28pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/5 "2023-06-02T20:28:24Z")

</div>

this will be very useful; Thanks lxp

---

<div class="post-metadata">

### Author: ![MegMac](https://resource.dopus.com/user_avatar/resource.dopus.com/megmac/32/51564_2.png) [@MegMac](https://resource.dopus.com/u/MegMac)
#### Post date: [June 5, 2023, 9:17pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/6 "2023-06-05T21:17:18Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![khalidhosain](https://resource.dopus.com/user_avatar/resource.dopus.com/khalidhosain/32/28729_2.png) [@khalidhosain](https://resource.dopus.com/u/khalidhosain)
#### Post date: [June 6, 2023, 12:29pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/7 "2023-06-06T12:29:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [June 6, 2023, 1:28pm UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/8 "2023-06-06T13:28:18Z")

</div>

> [@MegMac](#):
>
> add the folder name as a tag

No need for a script:

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

```

---

<div class="post-metadata">

### Author: ![MegMac](https://resource.dopus.com/user_avatar/resource.dopus.com/megmac/32/51564_2.png) [@MegMac](https://resource.dopus.com/u/MegMac)
#### Post date: [July 4, 2023, 1:30am UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/9 "2023-07-04T01:30:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [July 4, 2023, 4:33am UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/10 "2023-07-04T04:33:07Z")

</div>

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](https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Keywords_for_SetAttr_META.htm)

---

<div class="post-metadata">

### Author: ![khalidhosain](https://resource.dopus.com/user_avatar/resource.dopus.com/khalidhosain/32/28729_2.png) [@khalidhosain](https://resource.dopus.com/u/khalidhosain)
#### Post date: [July 4, 2023, 10:21am UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/11 "2023-07-04T10:21:58Z")

</div>

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

```auto
@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.

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [July 4, 2023, 10:27am UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/12 "2023-07-04T10:27:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![khalidhosain](https://resource.dopus.com/user_avatar/resource.dopus.com/khalidhosain/32/28729_2.png) [@khalidhosain](https://resource.dopus.com/u/khalidhosain)
#### Post date: [July 4, 2023, 10:31am UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/13 "2023-07-04T10:31:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lxp](https://resource.dopus.com/user_avatar/resource.dopus.com/lxp/32/33734_2.png) [@lxp](https://resource.dopus.com/u/lxp)
#### Post date: [July 4, 2023, 10:58am UTC](https://resource.dopus.com/t/adding-metadata-tags-based-on-different-parts-of-the-file-name/43122/14 "2023-07-04T10:58:20Z")

</div>

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.
