Is there a way to format the file name by discnumber?

Here is a piece of pseudo-code(I think that should help with understanding?)

discs = set()

for i in songs:
    cd.add(i.discnumber)

if len(discs)<=1:
    formatter='$num(%track%,2) - %title%'
else:
    formatter=r'Disc %discnumber%\$num(%track%,2) - %title%'

for i in songs:
    i.save(formatter)

Anyway, I want to realize
If the selected songs all have the same discnumber (or no discnumber)
Then use $num(%track%,2) - %title% formatting
If from more than one discnumber
then use Disc %discnumber%\$num(%track%,2) - %title% formatting

Is it possible to implement this logic inside mp3tag?
Or is there any api that would allow me to implement this in an external program?

You could try:
[%discnumber%\]$num(%track%,2) - %title%
which works for files that either have a discnumber - and for those the discnumber should be included in the path - or don't. And for those the discnumber should not be used for the path.

It's perfect for files that don't have a discnumber.
But some albums mark all files with discnumber 1
Maybe I can clear it out manually :thinking:

What does the discnumber look like for the ones with discnumber 1?
You could filter for these, e.g.:
NOT %discnumber% is "1/1"

There should be have 1/1.
I've also seen 01, or just 1.

does a simple "1" mean that there are no further discs? Or is the first of a set of discs?
If you know which formats should be excluded, then you can use all these in the filter expression.

fitlers can be appended with AND or OR.

This is the string I use that applies Disc-Track if there is more than one disc in a set. It also works whether or not the disc is something like 2 or 2/2. It also shortens each field to a max length of 80 characters to stay within the limits of some older OS.
..\Music\$left(%albumartistsort%,80)\$left(%albumsort%,80)\$ifgreater(%totaldiscs%,1,$left(%discnumber%,1)-,)$num(%track%,2) $left(%titlesort%,80)