# Generating %DISCNUMBER% Tags

**URL:** https://community.mp3tag.de/t/generating-discnumber-tags/4117
**Category:** Support
**Created:** [November 28, 2006, 2:05pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117 "2006-11-28T14:05:05Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![shawncruiksh](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@shawncruiksh](https://community.mp3tag.de/u/shawncruiksh)
#### Post date: [November 28, 2006, 2:05pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/1 "2006-11-28T14:05:05Z")

</div>

I'm trying to make MP3Tag automatically generate the discnumber field based on the folder structure. Can anyone help me out? I have very limited scripting knowledge - so I'm confused as to where I should even start...

I'd like MP3Tag to recognize if there is a folder in the parent directory named "CD1", "CD2", "CD3", etc., to automatically create a x/x tag in the discnumber field, correctly tagging which directory it is in - and generate a discnumber total.

Also - if there isn't - I would like it to tag 1/1 for the discnumber.

Can this be done?

Thanks very much! 😃

---

<div class="post-metadata">

### Author: ![JJ\_Johnson](https://community.mp3tag.de/user_avatar/community.mp3tag.de/jj_johnson/32/67_2.png) [@JJ\_Johnson](https://community.mp3tag.de/u/JJ_Johnson)
#### Post date: [December 7, 2006, 12:28am UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/2 "2006-12-07T00:28:50Z")

</div>

> [@shawncruiksh](#):
>
> I'm trying to make MP3Tag automatically generate the discnumber field based on the folder structure. Can anyone help me out? I have very limited scripting knowledge - so I'm confused as to where I should even start...
> 
> I'd like MP3Tag to recognize if there is a folder in the parent directory named "CD1", "CD2", "CD3", etc., to automatically create a x/x tag in the discnumber field, correctly tagging which directory it is in - and generate a discnumber total.
> 
> Also - if there isn't - I would like it to tag 1/1 for the discnumber.
> 
> Can this be done?

I'm guessing the hard part in creating discnumber tags 'x/y' would be the 'y' part - finding the discnumber total. I doubt if it can be done.

I organize my multidisc sets in exactly the same way and never felt it was worth the effort of creating an action that could read the folder to extract the disc number.

ArtistName/  
../AlbumName/  
../../Disc 1/  
../../Disc 2/

Since these albums make up a small percentage of my library, I just tag them manually. What I've found is that if you load up the whole album (all discs) Mp3tag will sort them quite nicely and it makes it easy to select all files on disc 1 and give them DISC and DISCC tags (what I use for Flac), then all files from the disc 2, etc. And since the majority of my multidisc sets are two discs, I've created two actions called 'Disc 1 of 2' and 'Disc 2 of 2' that can handle 99% of the cases.

---

<div class="post-metadata">

### Author: ![Gergith](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/g/d26b3c/32.png) [@Gergith](https://community.mp3tag.de/u/Gergith)
#### Post date: [January 17, 2007, 10:05pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/3 "2007-01-17T22:05:04Z")

</div>

why doesnt this work?:

$add(%discnumber% ,'/''2')

---

<div class="post-metadata">

### Author: ![dano](https://community.mp3tag.de/user_avatar/community.mp3tag.de/dano/32/6_2.png) [@dano](https://community.mp3tag.de/u/dano)
#### Post date: [January 17, 2007, 10:30pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/4 "2007-01-17T22:30:13Z")

</div>

$add() is a math function that adds two numbers and returns the sum.  
It's not made for string concatenation.

The thing that you want to achieve can be done with this action:

_Action type:_ **Format value**  
_Field:_ **Discnumber**  
_Formatstring:_ **%discnumber%/2**

---

<div class="post-metadata">

### Author: ![Logan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/278dde/32.png) [@Logan](https://community.mp3tag.de/u/Logan)
#### Post date: [January 18, 2007, 1:18am UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/5 "2007-01-18T01:18:38Z")

</div>

It would still be nice to have a way to automatically generate the '2' in the above example, the only thing I've been able to think of so far (using existing functionality) is to export a simple text file and read it back in as a taglist.

This is still very manual though.

---

<div class="post-metadata">

### Author: ![Ydope](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@Ydope](https://community.mp3tag.de/u/Ydope)
#### Post date: [January 18, 2007, 12:04pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/6 "2007-01-18T12:04:40Z")

</div>

> [@Logan](#):
>
> It would still be nice to have a way to automatically generate the '2' in the above example, the only thing I've been able to think of so far (using existing functionality) is to export a simple text file and read it back in as a taglist.
> 
> This is still very manual though.

Maybe you could replace the '2' with a regular expression that gives the last character of %\_directory% ?

---

<div class="post-metadata">

### Author: ![Logan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/278dde/32.png) [@Logan](https://community.mp3tag.de/u/Logan)
#### Post date: [January 18, 2007, 12:30pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/7 "2007-01-18T12:30:05Z")

</div>

> [@Ydope](#):
>
> Maybe you could replace the '2' with a regular expression that gives the last character of %\_directory% ?

Thanks for the respone.

I have a function that does that (need to change it to a RegExp), but when the expression runs you don't know if you are on the last directory or not, so the result would always be 1/1, 2/2 , 3/3, 4/4. You'd need to capture the largest number and go back and update the other tracks.

---

<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: [January 18, 2007, 2:39pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/8 "2007-01-18T14:39:41Z")

</div>

> [@shawncruiksh](#):
>
> ... I'd like MP3Tag to recognize if there is a folder in the parent directory named "CD1", "CD2", "CD3", etc., to automatically create a x/x tag in the discnumber field, correctly tagging which directory it is in - and generate a discnumber total.

You have a folder/file structure like this:

artist1/album1/cd1/files  
artist1/album1/cd2/files

artist1/album2/cd1/files  
artist1/album2/cd2/files  
artist1/album2/cd3/files

artist2/album1/files

You can try something like this:

1. Create an actiongroup 'Set DISCNUMBER step 1 (n/x)'  
Action #1:  
_Action type:_ **Format value**  
_Field:_ **DISCNUMBER**  
_Formatstring:_ **%\_DIRECTORY%**

Action #2:  
_Action type:_ **Replace with regular expression**  
_Field:_ **DISCNUMBER**  
_Regular expression:_ **^(.\*)(\d+)$**  
_Replace matches with:_ **$trim($2)/x**

[] case-sensitive comparison
2. Create an actiongroup 'Set DISCNUMBER step 2 (n/2)' Action #1: _Action type:_ **Replace** _Field:_ **DISCNUMBER** _Original:_ **x** _Replace with:_ **2** [] only as whole word [] case-sensitive comparison
3. Select all the tracks from album1 and run actiongroup 'Set DISCNUMBER step 1 (n/x)'.
4. Select all the tracks from album1 and run actiongroup 'Set DISCNUMBER step 2 (n/2)'.
5. Done.

You may create some more actiongroups of type 'step 2' with replacement numbers of 3 or 4 or what you want: 'Set DISCNUMBER step 2 (n/3)'. 'Set DISCNUMBER step 2 (n/4)'.

This is the half automatic way to work around the problem.  
It will be better to have a full automatic solution, ...  
but therefore we need an extra storage place to hold a user defined value while an action runs against the selected tracks, something like we know from the Mp3tag export section:  
$puts(myVar,221)  
$get(myVar)

An action that can accept user input on runtime would be nice too.

DD.20070118.1633

---

<div class="post-metadata">

### Author: ![Logan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/278dde/32.png) [@Logan](https://community.mp3tag.de/u/Logan)
#### Post date: [January 18, 2007, 4:39pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/9 "2007-01-18T16:39:34Z")

</div>

> [@DetlevD](#):
>
> You can try something like this:
> 
> This is the half automatic way to work around the problem.  
> It will be better to have a full automatic solution, ...  
> but therefore we need an extra storage place to hold a user defined value while an action runs against the selected tracks, something like we know from the Mp3tag export section:  
> $puts(myVar,221)  
> $get(myVar)
> 
> An action that can accept user input on runtime would be nice too.
> 
> DD.20070118.1633

This definately gets us closer to full solution, nice work!.

---

<div class="post-metadata">

### Author: ![Logan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/278dde/32.png) [@Logan](https://community.mp3tag.de/u/Logan)
#### Post date: [January 19, 2007, 1:27am UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/10 "2007-01-19T01:27:39Z")

</div>

I think that the process can simplified:

```
 1. Create an actiongroup 'Set DISCNUMBER (n/<i>x</i>)'

```

 

_Action type:_ **Format value**  
 _Field:_ **DISCNUMBER**  
 _Formatstring:_ **$regexp(%\_directory%,^(.\*)(\d+)$,$2)/_x_**

[] case-sensitive comparison 2. Create addtional action groups where x is the number of discs that the album contains.

---

<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: [January 19, 2007, 10:16am UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/11 "2007-01-19T10:16:36Z")

</div>

> [@Logan](#):
>
> I think that the process can simplified ...

Well, we can make it even a little bit more comfortable for different cases:

- '_CD 1_ The Best of all'
- 'The Best of all _CD1_'
- 'The Best _CD1_ of all'
- 'The Best _CD 1_ of all'

Important is, that there is a character group containing 'CD' in capital letters followed by a number.
1. Create an action group 'Set DISCNUMBER step 1 (n._x_)'

( :astonished: 'n/x' slash is not allowed in the title name ?!?!?) 

_Action type:_ **Format value**  
 _Field:_ **DISCNUMBER**  
 _Formatstring:_ **$regexp(%\_directory%,^.\*CD _(\d+)._$,$1)/_x_**  
  case-sensitive comparison

2. Create additional action groups 'Set DISCNUMBER step 2 (n._x_)' where **_x_** is the number of discs that the album contains, e.g.

Action #1:  
_Action type:_ **Replace**  
_Field:_ **DISCNUMBER**  
_Original:_ **x**  
_Replace with:_ **2**

[] only as whole word

case-sensitive comparison

You can select both action groups at once and run them against the selected tracks.  
 ![](https://community.mp3tag.de/uploads/default/original/2X/0/0284a174a98186a6b5e98d64d0b283334a2f49bb.jpg)  

DD.20070119.1218

![](https://community.mp3tag.de/uploads/default/original/2X/0/0284a174a98186a6b5e98d64d0b283334a2f49bb.jpg)

---

<div class="post-metadata">

### Author: ![Logan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/278dde/32.png) [@Logan](https://community.mp3tag.de/u/Logan)
#### Post date: [January 19, 2007, 12:55pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/12 "2007-01-19T12:55:01Z")

</div>

That helps even more, thanks. Can't we combine the two actions though? Instead of having the replace in the second step just create multiple versions of Step 1 with X being 1, 2, 3, etc.

In fact, I think we can add the $if in to create a single action for Disc 1/1 (default, no 'CD' in \_directory) and then have Disc x/2 be the else in the statement.

---

<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: [January 19, 2007, 2:24pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/13 "2007-01-19T14:24:07Z")

</div>

> [@Logan](#):
>
> That helps even more, thanks. Can't we combine the two actions though? Instead of having the replace in the second step just create multiple versions of Step 1 with X being 1, 2, 3, etc.

Yes, that's seems to be the most practicable way.

Action group: 'Set DISCNUMBER (n.2)'  
Action #1:  
_Action type:_ **Format value**  
_Field:_ **DISCNUMBER**  
_Formatstring:_ **$regexp(%\_directory%,^.\*CD _(\d+)._$,$1)/2**

Action group: 'Set DISCNUMBER (n.3)'  
Action #1:  
_Action type:_ **Format value**  
_Field:_ **DISCNUMBER**  
_Formatstring:_ **$regexp(%\_directory%,^.\*CD _(\d+)._$,$1)/3**

... and so on.

DD.20070119.1624

---

<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: [January 19, 2007, 2:55pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/14 "2007-01-19T14:55:06Z")

</div>

> [@Logan](#):
>
> ... In fact, I think we can add the $if in to create a single action for Disc 1/1 (default, no 'CD' in \_directory) and then have Disc x/2 be the else in the statement.

So this may work:  
Action #1:  
_Action type:_ **Format value**  
_Field:_ **DISCNUMBER**  
_Formatstring:_ **$if($eql($regexp(%\_directory%, \*(CD) \*\d+,$1,0),CD),$regexp(%\_directory%, \*CD \*(\d+),$1,0)/2,1/1)**

DD.20070119.1655

---

<div class="post-metadata">

### Author: ![Logan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/278dde/32.png) [@Logan](https://community.mp3tag.de/u/Logan)
#### Post date: [January 19, 2007, 3:18pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/15 "2007-01-19T15:18:36Z")

</div>

> [@DetlevD](#):
>
> So this may work:  
> Action #1:  
> _Action type:_ **Format value**  
> _Field:_ **DISCNUMBER**  
> _Formatstring:_ **$if($eql($regexp(%\_directory%, \*(CD) _\d_,$1,0),CD),$regexp(%\_directory%, \*CD _(\d_),$1,0)/2,1/1)**
> 
> DD.20070119.1655

Works perfectly, only change I made was to wrap the second RegExp in $num(re,1) in the event that the directory was listed as CD01.

Thanks for your help!

---

<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: [January 19, 2007, 3:35pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/16 "2007-01-19T15:35:33Z")

</div>

Hoping, that we have it now:

**$if($eql($regexp(%\_directory%,'^.\* \*(CD) \*\d+.\*$','$1',0),'CD'),$regexp(%\_directory%,'^.\* \*CD \*0\*(\d+).\*$','$1',0)'/2','1/1')**

DD.20070119.1735

---

<div class="post-metadata">

### Author: ![Logan](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/l/278dde/32.png) [@Logan](https://community.mp3tag.de/u/Logan)
#### Post date: [January 19, 2007, 3:46pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/17 "2007-01-19T15:46:03Z")

</div>

> [@DetlevD](#):
>
> Hoping, that we have it now:

Yep - I think we do.

---

<div class="post-metadata">

### Author: ![shawncruiksh](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@shawncruiksh](https://community.mp3tag.de/u/shawncruiksh)
#### Post date: [January 20, 2007, 4:01am UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/18 "2007-01-20T04:01:48Z")

</div>

Wow - I haven't checked this topic in forever. Thanks very much for all your hard work, guys!

Would it be possible for me to get you to alter this? My directory format is a bit different.

Here's how I have my directories:

Ben Harper - Both Sides Of The Gun (2006)\CD1\Files

Thanks again!

---

<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: [January 20, 2007, 8:23am UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/19 "2007-01-20T08:23:39Z")

</div>

> [@shawncruiksh](#):
>
> ... Would it be possible for me to get you to alter this? My directory format is a bit different. Here's how I have my directories:  
> Ben Harper - Both Sides Of The Gun (2006)\CD1\Files ...

Well, I know, that the problem is not solved in general so far (we really need a ready made _Discnumber Assistant_), but now we have a practical way to handle cases like ...

- If there is no folder 'CDn' between album and file:  
album/file
- If there are two folders 'CDn' between album and file:  
album/CD1/file  
album/CD2/file

... by using this  
Action #1:  
_Action type:_ **Format value**  
_Field:_ **DISCNUMBER**  
_Formatstring:_ **$if($eql($regexp(%\_directory%,'^.\* _(CD) \d+.$__','$1',0),'CD'),$regexp(%\_directory%,'^._ _CD 0(\d+)._$****','$1',0)'/2','1/1')**

If there is only one folder 'CD1' between album and file:  
album/CD1/file  
you can simply set DISCNUMBER to '1/1' by  
Action #1:  
_Action type:_ **Format value**  
_Field:_ **DISCNUMBER**  
_Formatstring:_ **1/1**

DD.20070120.1020

---

<div class="post-metadata">

### Author: ![shawncruiksh](https://community.mp3tag.de/letter_avatar_proxy/v4/letter/s/58956e/32.png) [@shawncruiksh](https://community.mp3tag.de/u/shawncruiksh)
#### Post date: [January 21, 2007, 7:23pm UTC](https://community.mp3tag.de/t/generating-discnumber-tags/4117/20 "2007-01-21T19:23:46Z")

</div>

Sorry - the problem I was having before was on my end. Another action was interfearing with it.

Either way, the script you posted works perfectly! Thanks very much! 😃

[Next page](https://community.mp3tag.de/t/generating-discnumber-tags/4117.md?page=2)
