Scripting Function that Derives DISCNUMBER from FOLDER

For multiple disc albums, such as box sets, I usually name the individual discs, as an album sub-folder, this way -- but only when each disc does not have its own specific subtitle:


Sometimes, when the name/title of the disc, as sub-folder, is known, I will add it so that the sub-folder title looks like this:

CD1. Title
CD2. Title
CD3. Title

Mostly, the first example applies.

I wish to derive the Field: DISCNUMBER, from the folder/directory by means of a suitable script. No leading zeroes are required. However, it will be nice to see an optional script, which provides for leading zeroes in the disc number as well. :slight_smile:

From what I have learned so far, I was able to construct a suitable Action Group, which were successful, but I am certain there is a much better, more efficient way to trim the two first two characters, "CD", so only the numerical character is displayed. Furthermore, I do not know how to construct a script to apply whenever I have to derive the disc number from the second set of examples given above.

Could you please advise me?

For your information, the Action Group I came up with so far:

Format Value: DISCNUMBER
Format String: %_directory%

Then, Replace with Regular Expression in:

Regular Expression: right(%discnumber%,2)
Replace matches with: Blank

The aforementioned failed, so I tried this Action Group, which was successful:

Format Value: DISCNUMBER
Format String: %_directory%

Replace Field: DISCNUMBER
Original: CD
x with case sensitive comparison enabled

Of course, because it is no regular expression.

What about an action of the type "Guess value" for _DIRECTORY
Guessing pattern: CD%discnumber%

With a little extension the same type of action also works for the second set of CDs:
an action of the type "Guess value" for _DIRECTORY
Guessing pattern: CD%discnumber%. %dummy%

Action "Format value" ... DISCNUMBER <== $if($eql($regexp(%_directory%,'^(CD|DISC|DISK)\s*0*(\d+).*$',,1),),$regexp(%_directory%,'^(CD|DISC|DISK)\s*0*(\d+).*$','$2',1),'1') $char(0)'If no match, then return 1, else return number from directory name'


... or just a 'quick and dirty' proposal ...

Apply Action "Format value" ... DISCNUMBER <== $regexp(%_directory%,'\D',)


Your 2nd action is missing the $ before right. But needs to be a format action.

But use this all in one action.

Use format action.

with: $regexp(%_directory%,.CD(\d+).,$1)

Edit: Noticed error with capture 1 in regexp

Thank you very much to all three of you, for your welcome advice. As you will have undoubtedly noticed already, I have zero in-depth knowledge and understanding of the subject at hand. The little bit I do have, I usually apply by trial and error. Mostly, I get along, but sometimes I am just so stuck, and then I am thankful that I can rely on experts like your good selves to assist me. I am always grateful for that.

Now I am off to go and try out your various proposals. :slight_smile:

Presently, the above script by DetlevD, seems to best fit the circumstance. There is, however, one drawback in my own application of it: When the script is applied, it will also number the single albums with disc number 1. I prefer to leave the disc number field of one-disc albums, blank/clean. Is it possible to revise the script to take care of this situation, while still allocating the appropriate individual disc number to multi-disc albums?

(Furthermore, is there another, perhaps simpler way, to derive each individual disc number from multi-disc albums- AND keep a single-disc album number blank? For instance, derive the disc number by counting the number of folders, and sequentially number them? In such a case, how would the correct number sequence be determined- if not from the folder description?). Please ignore the portion between brackets if it does not make sense. :slight_smile:

Thank you for your contribution.

Action "Format value" ... DISCNUMBER <== $if($eql($regexp(%_directory%,'^(CD|DISC|DISK)\s*0*(\d+).*$',,1),),$regexp(%_directory%,'^(CD|DISC|DISK)\s*0*(\d+).*$','$2',1),) $char(0)'If no match, then return nothing, else return number from directory n




... or ...



Thank you, DetlevD! :smiley:

Edit: I do not see see if, and how, this is related, but I am posting my findings after applying the new script:

When applying Tag-to-Filename, I use, among others, the following Action:

Format Value ===>_FILENAME ===>$replace([%discnumber%-]%track%. %title%,/,·,",'',:,··,(,{,),},)

Instead of naming the file

"01. Filename.ext"

the resulting filename now is

"-01. Filename.ext"

How do I correct this Action to achieve the desired filename format for both single and multi-disc albums? For example:

"01. Filename.ext"

"02. Filename.ext"

"1-01. Filename.ext"

"2-01. Filename.ext"

Thank you.

There is a writing error in the $replace function, so maybe this works ...

$replace([%DISCNUMBER%'-']%TRACK%'. '%TITLE%,'/','·','"','''',':','··','(','{',')','}')


Thank you for spotting the error, DetlevD. I have never used the hyphens, and yet it still seemed to work in other Actions, which I have used up to now. Are these hyphens an imperative, and how does one tell when to use them, or not? Is it perhaps discussed somewhere so I can read more about its application?

Edit: I have now tried the revised Action, but the result is identical. I think I may have lost the plot completely, and I have to check all the Actions in this Group again. It definitely adds a blank space in the beginning of the file name:

-01. Filename.txt (The "" representing the blank space)

Edit 2: If I do not apply this Action:

The file names are named in the desired manner i.e. without the preceding blank space and the dash- as it did before the Action above was changed.

You have to check, whether there is a tagfield involved, which is named DISCNUMBER, which carries one or more space character?


Thank you. Not yet sure why, but while looking for that elusive space character, the problem disappeared. I could have edited something inadvertently.

The need for the single hyphens in my replace function still baffles me, because the replace function seems to be working just fine without it.

I want to mention:

The hyphen character U+002D (-) is a punctuation mark used to join words and to separate syllables of a single word.
See also ...

The typewriter apostrophe character U+0027 (') is a punctuation mark used for several purposes.
See also ...

The Mp3tag function $replace is defined as ...
... or ...
$replace(string,from1,to1,from2,to2[,... and so forth ...])


Sorry, I truly messed up my vocabulary in my post. When speaking about the hyphen, I was in fact referring to the use of the apostrophe. :blush:

Thank you for correcting me, DetlevD. :slight_smile:

DetlevD: The error was not eliminated, as I had previously thought. The first of the above two scripts somehow inserts a space into the DISCNUMBER tag. It applies a space for single-album discs, and space after the disc number too, for multi-disc albums. I do not know how to edit the regular expression to care of this problem. That said, I eventually tested the second one of the above two scripts, and so far it has performed splendidly. However, I will still appreciate it very much if you could show me where the first of the two expressions happened to insert the space into the DISCNUMBER field.

Thank you.

Because there is no space character at all within the given format string, the space character, which you have detected, may probably be inserted by a %variable% or simply by writing error, ... and at all, who ever knows, beside you, how the involved folder names are spelled?


How would one apply this idea in reverse?

What I am looking at doing is moving and relabeling as required however I already have the Disc Numbers but would like to include existing Folder Names (if applicable) as well for example if I have the following:

Disc 1 (Classic Album)
Disc 2 (Remix Album)

from something like this for example: ( )

What I am trying to do is Create the above without having to rename the "album"? Am guessing, that I am going to have to create a user defined field and than apply it afterwards something like

If _directory is number and word for disc than user defined filed is word
move directory to new location and format again multidisc as required however put back in the user defined field

something like

Disc $num(%discnumber%,2)\%_filename_ext%,%discnumber%%userdefined%\%_filename_ext%

or is there an alternative?

With special thanks to ohrenkino I was able to come up with the following that works for the original idea. What it does is it generates folders based off Disc numbers and names (which omits any empty disc numbers) but how do I combine both?

Format Filename>
$ifgreater($len(%discnumber%),0,$ifgreater(%discnumber%,0,Disc $num(%discnumber%,2)\%_filename_ext%,%discnumber%\%_filename_ext%),$num(%track%,2) - %title%)

If _directory is number and word for disc than user defined filed is word
move directory to new location and format again multidisc as required however put back in the user defined field

something like

Disc $num(%discnumber%,2)\%_filename_ext%,%discnumber%%userdefined%\%_filename_ext%


So I have come up with the following work around using the above idea...and it works somewhat..
Format Value LABEL>%_directory%
Replace with regular expression
replaced with " "

Disc $num(%discnumber%,2)\%_filename_ext%,%discnumber%%label%\%_filename_ext%

I would like it to not process anything if the folder does not have any words following the (CD|DISC|DISK)\s*[0-9]+\s+ however this pulls any folder information such in this case it pulls any wording in the Folder...which isn't quite what I had in mind...looking to pull only whatever follows (CD|DISC|DISK)\s*[0-9]+\s+ and if nothing than do nothing..