# Extract year from directory name

**URL:** https://community.mp3tag.de/t/extract-year-from-directory-name/45662
**Category:** Support
**Created:** [June 12, 2019, 1:52am UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662 "2019-06-12T01:52:53Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![TheRadster](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/2bfe46/32.png) [@TheRadster](https://community.mp3tag.de/u/TheRadster)
#### Post date: [June 12, 2019, 1:52am UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662/1 "2019-06-12T01:52:53Z")

</div>

I want assign to the %year% tag the first value found in %\_directory% that starts with 19 or 20 and include the following two digits (to form the year). I have been trying to use the Format Value action.

$regexp(x,expr,repl)  
replaces the pattern specified by the regular expression expr in the string x by repl. The  
fourth optional parameter enables ignore case (1) or disables the ignore case setting (0).  
Please note that you have to escape comma and other special characters in expr

My directories have names like:  
1991 - Eric Clapton - 24 Nights  
The Turtles - 20 Greatest Hits (1984)  
1985 - George Benson - 20-20

Field=YEAR  
Format string=$regexp(%\_directory%,'(\b\d{4}\b)','$1') returns %\_directory% (no change)

Format string=$regexp(%\_directory%,'(\d{4})','$1') returns %\_directory% (no change)

Format string=$regexp(%\_directory%,'(19|20)\d{2}','$1') returns the original %\_directory% string with the 2 chars after 19 or 20 stripped.

Format string=$regexp(%\_directory%,'\b(19|20)\d{2}\b','$1') returns the original %\_directory% string with the 2 chars after 19 or 20 stripped.

Format string=$regexp(%\_directory%,\b\d{4}\b,'$1') returns everything but the year that I want! The year is stripped. I want exactly the opposite of this, I want the year as the result. This is a very simple regex and I don't know why it returns the opposite of what I want. If I remove the \b's the result is the same.

Result (opposite of what I want!):

- Eric Clapton - 24 Nights  
The Turtles - 20 Greatest Hits () (FLAC+CUE)
- George Benson - 20-20

I'm not sure how to do the parenthesis grouping and using the $1 stuff, may be the trick.  
The below example should be perfect, but my results are the opposite of expected.  
It has: 1. the stuff I want to ignore (not in parenthesis), 2. the stuff I want (in parenthesis), and 3. the stuff I don't want at the end (not in parenthesis).

I checked similar posts and one user had success with the regular expression: $regexp(%YEAR%,'^._(\d\d\d\d)._$','$1')

> [@Extracting year regardless of date format](https://community.mp3tag.de/t/extracting-year-regardless-of-date-format/16517):
>
> I've been using Format String: $left(%year%,4) to clean up my Year field, but is there a better action that could locate 4 consecutive digits in the YEAR tag (regardless of where they are located within the string) and then use just those 4 consecutive digits as the year (deleting all other characters from the field). This would allow the year to be extracted regardless of how the date has been formatted. Thanks.

I tried this and it gave me everything but the year (year had been stripped) and got result.

Try my examples. Do you get the same result? Can anyone figure this out?  
It is very useful to grab years from the directory name.  
Is is broken? This should be simple.  
Please help!

---

<div class="post-metadata">

### Author: ![TheRadster](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/2bfe46/32.png) [@TheRadster](https://community.mp3tag.de/u/TheRadster)
#### Post date: [June 12, 2019, 3:03am UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662/2 "2019-06-12T03:03:26Z")

</div>

Got it!  
To extract just the year from the directory name:  
Needed to add the ^.\* at the beginning, before the match, and .\*$ at the end after the match and have only the match part in parenthesis! The inner parenthesis are to select only years starting with 19 or 20, the outer set of parenthesis is the grouping for '$1'.

`$regexp(%_directory%,'^.*((19|20)\d{2}).*$','$1')` \<= This gives 19xx or 20xx year  
`$regexp(%_directory%,'^.*(\d\d\d\d).*$','$1')` \<= This gives 4 digit year  
`$regexp(%_directory%,'^.*(\d{4}).*$','$1')` \<= This gives 4 digit year

Note for the commands above, when I viewed the post the asterisks are being dropped.  
There should be carot dot asterisk before your (match) and dot asterisk dollar-sign at the end.

$regexp(%\_directory%,'\b\d{4}\b','$1') \<= Opposite: Directory name with year removed

Works for me now!

Even better result is to use %\_folderpath% rather than %\_directory% and when no match is found to blank the year.

Here's the improved formula: (you should see an asterisk after each of the dots, if you paste this formula and there are no asterisks, edit the formula and put them there).  
`$if($isdigit($regexp(%_folderpath%,'^.*((19|20)\d{2}).*$','$1')),$regexp(%_folderpath%,'^.*((19|20)\d{2}).*$','$1'),)`

---

<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 12, 2019, 5:22am UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662/3 "2019-06-12T05:22:10Z")

</div>

> [@TheRadster](#):
>
> Note for the commands above, when I viewed the post the asterisks are being dropped.

If you want to get text the way you typed it and no interpretation as formatting instruction then use the accent grave (top-left to bottom-right) to enclose that string, like `string`

---

<div class="post-metadata">

### Author: ![Darr247](https://community.mp3tag.de/user_avatar/community.mp3tag.de/darr247/32/380_2.png) [@Darr247](https://community.mp3tag.de/u/Darr247)
#### Post date: [June 12, 2019, 3:32pm UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662/4 "2019-06-12T15:32:56Z")

</div>

```
Needed to add the ^.* at the beginning, before the match, and .*$ at the end
```

---

<div class="post-metadata">

### Author: ![Darr247](https://community.mp3tag.de/user_avatar/community.mp3tag.de/darr247/32/380_2.png) [@Darr247](https://community.mp3tag.de/u/Darr247)
#### Post date: [June 12, 2019, 3:35pm UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662/5 "2019-06-12T15:35:13Z")

</div>

I used the 'Preformatted text' tool ( **\</\>** button, or **Ctrl** + **Shift** + **C** ) for that.

---

<div class="post-metadata">

### Author: ![Texta](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/t/85f322/32.png) [@Texta](https://community.mp3tag.de/u/Texta)
#### Post date: [February 24, 2025, 7:18pm UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662/6 "2025-02-24T19:18:21Z")

</div>

Thanks for this post - as always there's something more mp3tag can do and someone to push at least into the right direction. Did this manually for years (decade?).

---

<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: [March 26, 2025, 7:18pm UTC](https://community.mp3tag.de/t/extract-year-from-directory-name/45662/7 "2025-03-26T19:18:55Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
