Set metadata: track, title and artist from filename

The following code allows you to set the metadata: track, title and artist from the file name in format:
##. Title - Artist

When I select a single file everything works fine; but when I select several files, the data from the first file is applied to all the other files, could you help me so that each file receives its own data?

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    if (!tab.selected_files) return;

    var item = tab.selected_files(0);

    var name = item.name_stem;
    var name_track = name.replace(/(^[0-9]*)(.*)/, "$1");
    var name_title = name.replace(/(^[0-9\.\s]*)(.*)(\s-\s)(.*)/, "$2");
    var name_artist = name.replace(/(^[0-9\.\s]*)(.*)(\s-\s)(.*)/, "$4");

    if (tab.selected_files.count >= 2) {

        cmd.RunCommand('SetAttr META="track:' + name_track + '"');
        cmd.RunCommand('SetAttr META="title:' + name_title + '"');
        cmd.RunCommand('SetAttr META="artist:' + name_artist + '"');

    } else {

        cmd.RunCommand('SetAttr' +
            ' FILE="' + item + '"' +
            ' META "track:{dlgstring|Pista:|' + name_track + '}"');

        cmd.RunCommand('SetAttr' +
            ' FILE="' + item + '"' +
            ' META "title:{dlgstring|Título:|' + name_title + '}"');

        cmd.RunCommand('SetAttr' +
            ' FILE="' + item + '"' +
            ' META "artist:{dlgstring|Artista:|' + name_artist + '}"');

    }
}

You need to stuff your code into a loop like this:

for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
    var item = e.item();


}

What am I doing wrong now? :thinking:

    if (tab.selected_files.count >= 2) {

        for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
            var item = e.item();

            cmd.RunCommand('SetAttr META="track:' + name_track + '"');
            cmd.RunCommand('SetAttr META="title:' + name_title + '"');
            cmd.RunCommand('SetAttr META="artist:' + name_artist + '"');
        }

It needs to look a bit more like this.

// https://resource.dopus.com/t/set-metadata-track-title-and-artist-from-filename/48101

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    cmd.deselect = false;

    if (!tab.selected_files) return;

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();

        var name = item.name_stem;
        var name_track = name.replace(/(^[0-9]*)(.*)/, "$1");
        var name_title = name.replace(/(^[0-9\.\s]*)(.*)(\s-\s)(.*)/, "$2");
        var name_artist = name.replace(/(^[0-9\.\s]*)(.*)(\s-\s)(.*)/, "$4");

        cmd.RunCommand('SetAttr' +
            ' FILE="' + item + '"' +
            ' META "track:{dlgstring|Pista:|' + name_track + '}"');

        cmd.RunCommand('SetAttr' +
            ' FILE="' + item + '"' +
            ' META "title:{dlgstring|Título:|' + name_title + '}"');

        cmd.RunCommand('SetAttr' +
            ' FILE="' + item + '"' +
            ' META "artist:{dlgstring|Artista:|' + name_artist + '}"');

    }
}

Correct, but the ideal would be that when several files are selected none of the windows are shown, that is, the metadata is established directly, showing the windows only when a single file is selected