Help with the renaming template

I need to rename the subfolder CD, which has the following explanations

CD1 - Album

%artist%%year% - %album%\CD%discnumber% - %discsubtitle%%dummy%

So it turns out

But if there is no explanation in the name of the folder with a title "CD", then the script does not work, how to do it for both options at the same time

*I don't know where the backslash went

If you enclose the part which could be there or be absent in square brackets, you have some kind of IF condition:
%artist%\%year% - %album%\[CD%discnumber% - ]%discsubtitle%%dummy%
The use of %dummy% does not make any sense when writing a filename.

Your idea about square brackets is clear and correct, but it does not work (((( I tried different combinations of parenthesis and the second part, including

Without a dummy substitution if there are files inside the last folder it doesn't work at all

I agree that the way that you applied the format string does not work at all.
If you want to rename a folder, you have to use
Action "Format value"

This renames the folder and therefore also moves all the files in that folder to the new location.
If the number of source folders is smaller than the number of target folders, then you have to define for each file where it should belong. Then you have to use
with a mask that also defines the filename ("%dummy%" in this respect means "nothing" and empty filenames are not allowed)
Alternatively, Converter>Tag-Tag or Action "Format value" for _FILENAME, also with a mask that defines the full filename.

Excuse me, I am using google translator, now I re-read what I myself wrote, the wrong question on my part. I need to get tags from filenames, I'm good when there is no nested structure. When there are folders CD1 and CD2, then problems arise, especially when one folder is swept through a dash CD1 - bls bla bla

To import data from a source with different patterns you need one pattern each.
You can filter for files that have "CD" in the path with e.g.
%_path% MATCHES CD\d+
or for all those that do not have "CD" in the path:
NOT %_path% MATCHES CD\d+
Like that you can import the data with "CD" in the path with a dedicated pattern.

Forgive my wildness, I did not understand exactly how to apply this in the program. Do you suggest first sorting by nesting, and then renaming the groups with different scripts?

Yes (I think).
First filter for files that have CD in the filename
%_path% MATCHES CD\d+
and import the data with a mask like
%artist%\%year% - %album%\CD%discnumber% - %discsubtitle%\%dummy%

then filter for files that do not have CD in the filename
NOT %_path% MATCHES CD\d+
and import the data with a mask like
%artist%\%year% - %album%\%discsubtitle%\%dummy%

You cannot have 1 mask for 2 different patterns.

Tell me how to set up sorting - this option does not work

It's not sorting, it's filtering.
Press F3 to see where you can enter the filter.

Yes, it happened. And can this %_path% MATCHES CD\d+ be added to the sorting or saved somehow, so as not to re-enter the data in the filter string?

As the filter reduces the number of displayed files it does not make sense to enter this string in a column definition. So the answer is: no, it cannot be added to sorting (as it is filtering).
Filter expressions are saved automatically in the history for filters and can be accessed with the dropdown list for filters.

Thank you for your help, I will use your method