# Script Query: Extracting Text Inside Square Brackets, From Directory Name

**URL:** <https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421>\
**Category:** Support\
**Created:** [June 23, 2020, 11:53pm UTC](https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421 "2020-06-23T23:53:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamyou](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@iamyou](https://community.mp3tag.de/u/iamyou)\
**Post date:** [June 23, 2020, 11:53pm UTC](https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421/1 "2020-06-23T23:53:35Z")

</div>

I'm a long-time fan of MP3Tag,  
but recently, I've been unlocking more of the power of MP3Tag 🙂

My query:

I have many folders, e.g.

James - Album1-[**LMNOP123**]-2019  
Smith\_-\_Album\_2-[**XYZ3210**]-2017

Is there a script function, to extract the text within the square brackets "[" and "]" of a folder,  
and write this text into the tag ?

Example:  
**LMNOP123** -\> written into 'Comments' field  
or  
**XYZ3210** -\> written like this " XYZ3210 - %publisher% " (into 'Comments field' )

I'm just having issues, trying to 'select' text, inside square-brackets  
and most functions are for trim/cut only or the other functions, rely on specifying the 'n' (number) of characters, but as you can see the folder names all have varying length, some with spaces, some with underscores but all with text inside the square brackets.

---

<div class="post-metadata">

**Author:** ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)\
**Post date:** [June 24, 2020, 4:27am UTC](https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421/2 "2020-06-24T04:27:36Z")

</div>

Try Convert\>Filename-Tag  
Format string: `%dummy%[%comment%]%dummy%\%dummy%`

---

<div class="post-metadata">

**Author:** ![iamyou](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@iamyou](https://community.mp3tag.de/u/iamyou)\
**Post date:** [June 24, 2020, 5:38am UTC](https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421/3 "2020-06-24T05:38:20Z")

</div>

> [@ohrenkino](#):
>
> Format string: `%dummy%[%comment%]%dummy%\%dummy%`

wow.... **ohrenkino** , your solution and it works!  
in fact, it's 'too simple', that i should have thought of that 🤔

it's basically, what i was after!

Now, out of curiousity, is it possible to grab certain part of the folder,  
e.g. Smith\_-\_Album\_2-[**XYZ3210**]-2017

and then inserting that, as part of another tag,  
i.e.  
**XYZ3210** -\> written like this " XYZ3210 - %publisher% " (into 'Comments field' )  
or  
**XYZ3210** --\> written like this " XYZ Productions -- **XYZ3210**" (into 'Publisher field')

Basically, if there's something in 'Publisher' already,  
then XYZ3210 is the catalog number, within that Publisher or Record Label,  
as if the text in the square-brackets is a suffix to whatever already inside Publisher.

I don't know if there's a "Catalog" ID3 tag, but this is just my way of grabbing catalog-numbers into ID3 tags.

---

<div class="post-metadata">

**Author:** ![ohrenkino](https://community.mp3tag.de/user_avatar/community.mp3tag.de/ohrenkino/32/4843_2.png) [@ohrenkino](https://community.mp3tag.de/u/ohrenkino)\
**Post date:** [June 24, 2020, 5:50am UTC](https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421/4 "2020-06-24T05:50:48Z")

</div>

There are, of course, several ways that may lead to the desired outcome.  
There is a 1-step-way with an action of the type "Guess value"  
Source pattern: `$regexp(%_directory%,'.*\[(.*)\].*',$1) - %publisher%`  
Target string: %comment%  
or, if the target is publisher:  
Source pattern: `%publisher% - $regexp(%_directory%,'.*\[(.*)\].*',$1)`  
Target string: %publisher%

Or you import the catalog no. into a user-defined field (e.g. MY\_CATNO) with the convert-function,  
then add the content of MY\_CATNO to the repective field with an action of the type "Format value" e.g.  
Format value for PUBLISHER:  
Format string: %publisher% - %MY\_CATNO%  
and after that you can delete MY\_CATNO

---

<div class="post-metadata">

**Author:** ![iamyou](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@iamyou](https://community.mp3tag.de/u/iamyou)\
**Post date:** [June 25, 2020, 12:31am UTC](https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421/5 "2020-06-25T00:31:39Z")

</div>

ohrenkino,  
That's exactly what I was looking for.  
You've saved me (again) !

I didn't understand the "Guess Value" action,  
until the way you showed it to me.

Your solution is simple & elegant.  
I can build upon that now 🙂

---

<div class="post-metadata">

**Author:** ![system](https://community.mp3tag.de/uploads/default/original/2X/c/ce7035d426cb755a7916793326d23b465222a407.png) [@system](https://community.mp3tag.de/u/system)\
**Post date:** [February 9, 2026, 12:28pm UTC](https://community.mp3tag.de/t/script-query-extracting-text-inside-square-brackets-from-directory-name/49421/6 "2026-02-09T12:28:40Z")

</div>


