# Easy way to get directory names above Parent?

**URL:** https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324
**Category:** General Discussion
**Created:** [November 12, 2009, 7:20pm UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324 "2009-11-12T19:20:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![tjwhale](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@tjwhale](https://community.mp3tag.de/u/tjwhale)
#### Post date: [November 12, 2009, 7:20pm UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/1 "2009-11-12T19:20:01Z")

</div>

Hi,

I've been using mp3tag for a while, and very much love it! Especially after updating recently and finding the few annoyances I'd lived with had gone away. Yesterday I discovered 'Actions' and decided to try my hand at further automating my file tagging. I've got most of what I want working, but I'm wondering if there is an easy way to get the directory name that is one above \_Parent\_Directory? I save files in the following formats:

 

%artist%\%album%\%track% - %title%  
%artist%\%year% - %album%\%track% - %title%  
%artist%\%album% [nCD]\CDn\%track% - %title%  
%artist%\%album% [nnCD]\CDnn\%track% - %title%  
%artist%\%year% - %album% [nnCD]\CDnn (subalbum title)\%track% - %title%

 

and so on, using CD1, CD2 etc for multiple disk albums.

 

So sometimes I want to get the artist from the second directory back (which I get with \_Parent\_Directory), and sometimes the third. Is there an easier way than... Come to think of it, how can I search from the right for the correct occurrance of ""?

 

If this is not so easy, then I'll make a suggestion for a function that would return the x higher directory ![:music:](https://community.mp3tag.de/uploads/default/original/1X/2ba05d1ade5eb0d15fc173be6f717dbe63890a70.gif ":music:")

 

Thanks for any help. Mp3tag is really a great piece of software! I used to follow the Vim development when I used it at work, and now I have a substitute obsession for home ![:sorcerer:](https://community.mp3tag.de/uploads/default/original/1X/73535d0e07591636841ac7aadccc8f7ff56e7807.gif ":sorcerer:")

 

Terry

---

<div class="post-metadata">

### Author: ![Quaraxkad](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/q/7bcc69/32.png) [@Quaraxkad](https://community.mp3tag.de/u/Quaraxkad)
#### Post date: [November 13, 2009, 1:01am UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/2 "2009-11-13T01:01:01Z")

</div>

You could use %\_folderpath% for the entire directory structure, plus the functions $strrchr(x,y) and $right(x,n) to extract only the folder level you're looking for.

---

<div class="post-metadata">

### Author: ![tjwhale](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@tjwhale](https://community.mp3tag.de/u/tjwhale)
#### Post date: [November 13, 2009, 5:29am UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/3 "2009-11-13T05:29:11Z")

</div>

I was looking at that. $strrchr and $strchr only find the last or first occurrance of a character, so I'd have to successively strip off lower directories. Are there such things as user variables or do I have to use unused tags? I'm thinking I'd have to use $left twice to get to the right place, then $right to pluck out the directory I want, but I'd also need the string lengths of the intermediates, and thus the intermediate storage and multiple Format Value entries. Arggg. These long strings hurt my head.

> [@Quaraxkad](#):
>
> You could use %\_folderpath% for the entire directory structure, plus the functions $strrchr(x,y) and $right(x,n) to extract only the folder level you're looking for.

---

<div class="post-metadata">

### Author: ![Quaraxkad](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/q/7bcc69/32.png) [@Quaraxkad](https://community.mp3tag.de/u/Quaraxkad)
#### Post date: [November 13, 2009, 6:00am UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/4 "2009-11-13T06:00:11Z")

</div>

> [@tjwhale](#):
>
> I was looking at that. $strrchr and $strchr only find the last or first occurrance of a character, so I'd have to successively strip off lower directories. Are there such things as user variables or do I have to use unused tags? I'm thinking I'd have to use $left twice to get to the right place, then $right to pluck out the directory I want, but I'd also need the string lengths of the intermediates, and thus the intermediate storage and multiple Format Value entries. Arggg. These long strings hurt my head.

You won't need to use variables to do that. You can nest the functions, the result will be a pretty convoluted string of functions, but it can work.

I should have suggested Regular Expressions though. You should probably use $regexp(string,regex). A _working_ regex to get the last folder is "**._\\([^\\]+)\\_**_" (no quotes). Add "**(.**_**?\\){1}**" to the end to get the second to last folder, change it to "**(.\*?\\){2}**" to get the third to last, etc. These don't actually work in $regexp() though, because I'm not sure how to format that regex to work inside the mp3tag function (does it need quotes? escape chars? The help file is completely unclear on this). The regex DOES work though, you can confirm it on [http://gskinner.com/RegExr/](http://gskinner.com/RegExr/), go to the Replace tab, type your folder string into the first big box, put the regex in the first small box and "$1" in the second small box.

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [November 13, 2009, 6:28am UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/5 "2009-11-13T06:28:15Z")

</div>

> [@tjwhale](#):
>
> ... Arggg. These long strings hurt my head.

Although Mp3tag has no concrete list oriented functions, like ItemCount or ItemExtract, there might be a trick to simulate such functionality.

"Format value" a temporary tag field e. g. MYFOLDERPATH with the value  
$replace(%\_FOLDERPATH%,'','\\')  
This should split the folder path into its components and create multiple MYFOLDERPATH tag fields in extended tag view dialog.

Using scripting function $meta(MYFOLDERPATH,N) you can fetch the Nth item from the folder path.

Using scripting function $meta(MYFOLDERPATH,$sub($folderdepth(%\_FOLDERPATH%),N)) you can fetch the Nth item (zero based) from the right side of the folder path.

After all is done you can remove the temporary tag field.

DD.20091113.0825.CET

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [November 17, 2009, 10:41am UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/6 "2009-11-17T10:41:41Z")

</div>

Following example of an Action Group "SplitFolderpath" demonstrates how to split the content of the Mp3tag system variable \_FOLDERPATH into it's folder components and make the folder names available for scripting by owner defined tag fields.

MY\_FOLDER\_0 is the parent folder of the related file.  
MY\_FOLDER\_1 is the next folder up.  
MY\_FOLDER\_2 is the folder two steps up.  
... and so on.

Begin Action Group **SplitFolderpath**

Action #1  
_Actiontype 5:_ **Format tag field**  
_Field:_ **MY\_FOLDERPATH**  
_Formatstring:_ **%\_FOLDERPATH%**

Action #2  
_Actiontype 16:_ **Split fields by separator**  
_Field:_ **MY\_FOLDERPATH**  
_Separator:_ **\</b\>**

Action #3  
_Actiontype 5:_ **Format tag field**  
_Field:_ **MY\_FOLDER\_0**  
_Formatstring:_ **$meta(MY\_FOLDERPATH,$sub($folderdepth(%\_FOLDERPATH%),0))**

Action #4  
_Actiontype 5:_ **Format tag field**  
_Field:_ **MY\_FOLDER\_1**  
_Formatstring:_ **$meta(MY\_FOLDERPATH,$sub($folderdepth(%\_FOLDERPATH%),1))**

Action #5  
_Actiontype 5:_ **Format tag field**  
_Field:_ **MY\_FOLDER\_2**  
_Formatstring:_ **$meta(MY\_FOLDERPATH,$sub($folderdepth(%\_FOLDERPATH%),2))**

Action #6  
_Actiontype 5:_ **Format tag field**  
_Field:_ **MY\_FOLDER\_3**  
_Formatstring:_ **$meta(MY\_FOLDERPATH,$sub($folderdepth(%\_FOLDERPATH%),3))**

Action #7  
_Actiontype 9:_ **Remove fields**  
_Fields to remove (semicolon separated):_ **MY\_FOLDERPATH**

End Action Group **SplitFolderpath** (7 Actions)

DD.20091117.1241.CET

---

<div class="post-metadata">

### Author: ![tjwhale](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@tjwhale](https://community.mp3tag.de/u/tjwhale)
#### Post date: [November 17, 2009, 3:33pm UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/7 "2009-11-17T15:33:56Z")

</div>

**Quaraxkad** :

Thanks for the regexp info. I ended up doing pretty much what you said, in two steps (2 separate Format Value's).

The first strips the path of the last two directories (I previously set up discnumber to be nonzero if I need to look for the grandparent directory instead of the parent):  
ARTIST := $if($eql(%discnumber%,0),%\_parent\_directory%,$regexp(%\_folderpath%,'\\[^\\]+\\[^\\]+\\ ,))

The second strips off everything up to the last directory in the previous result:  
ARTIST := $if($eql(%discnumber%,0),%artist%,$regexp(%artist%,'.\*\\',))

**DetlevD** :

I'm a little confused. Did your second message override the first? The bit about using $replace to create an array of MYFOLDERPATH entries doesn't compute, but your use of Action "split fields by separator" does. Without $folderdepth this method would be harder:) It should be documented in the list of scripting functions. It's good to be aware of the meta function though, I had overlooked it. Are the above (replace?, split fields) the only ways to get multiple occurrences of a field for meta to act upon?

Thanks all for your help.

Terry

---

<div class="post-metadata">

### Author: ![DetlevD](https://community.mp3tag.de/user_avatar/community.mp3tag.de/detlevd/32/123_2.png) [@DetlevD](https://community.mp3tag.de/u/DetlevD)
#### Post date: [November 17, 2009, 5:00pm UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/8 "2009-11-17T17:00:15Z")

</div>

> [@tjwhale](#):
>
> ... I'm a little confused. Did your second message override the first? The bit about using $replace to create an array of MYFOLDERPATH entries doesn't compute, but your use of Action "split fields by separator" does. ...

I've tried the first proposal in a manual way step by step and because it did work for me I thought it could be worth to throw it in and see what will happen.  
Hmm, maybe the first proposal needs a [Ctrl]+[S] saving or so to make the splitting permanent.

I like my second proposal, because it is clean and efficient and easy to understand.

> [@tjwhale](#):
>
> ... Without $folderdepth this method would be harder:) It should be documented in the list of scripting functions. It's good to be aware of the meta function though, I had overlooked it. ...

**$folderdepth(%\_folderpath%) ... can be simulated by ... $sub($sub($len(%\_folderpath%),$len($replace(%\_folderpath%,'\',))),1) ... or ... $sub($len(%\_folderpath%),$len($replace(%\_folderpath%,'\',)),1) ... or ... $sub($len($regexp(%\_folderpath%,'[^\\\\]',)),1)**

> [@tjwhale](#):
>
> ... Are the above (replace?, split fields) the only ways to get multiple occurrences of a field for meta to act upon? ...

As I understand the $meta(...) functions, those functions can only handle and work with an instantiated multi value tag field, but cannot handle a simple string which comes along as a delimited list.

DD.20091117.1900.CET, DD.20150116.1932.CET

---

<div class="post-metadata">

### Author: ![tjwhale](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/a9adbd/32.png) [@tjwhale](https://community.mp3tag.de/u/tjwhale)
#### Post date: [November 17, 2009, 6:26pm UTC](https://community.mp3tag.de/t/easy-way-to-get-directory-names-above-parent/9324/9 "2009-11-17T18:26:31Z")

</div>

> [@DetlevD](#):
>
> $folderdepth (%\_FOLDERPATH%) can be simulated e. g. by  
> $sub($sub($len(%\_FOLDERPATH%),$len($replace(%\_FOLDERPATH%,'',))),1)

Very cool string. Thanks!
