Need Help for better loop code

Hi dear community
I want to update multiple selected flac files artist track title from the file name. my example files name is:

Michael Jackson - 01 - just beat it.flac
Celine Dion - 02 - My Heart Will Go On.flac
Jennifer Lopez - 03 - Waiting For Tonight (Remix).flac
So the pattern is artist - track - title
I have found a code in this forum & customize that. the code is

@script jscript

function OnClick(data){
	var files = data.func.sourcetab.selected_files;
	var cmd = data.func.command; cmd.ClearFiles();
	var re = new RegExp("(.*) - ([0-9]*) - (.*)(\.mp3|\.flac)");
	for(var i=0;i<files.count;i++){
		DOpus.Output("File: [" + files(i).name+"]");
		var matches = String(files(i).name).match(re);
		if (!matches){ DOpus.Output("    skipped"); continue; }
		var artist = matches[1]; //var title = matches[2];
		var cmdli = 'SetAttr META="artist:'+artist+'" FILE="'+files(i).realpath+'"';
		DOpus.Output("    cmd: " + cmdli);
		cmd.RunCommand(cmdli);
	}

	for(var i=0;i<files.count;i++){
		DOpus.Output("File: [" + files(i).name+"]");
		var matches = String(files(i).name).match(re);
		if (!matches){ DOpus.Output("    skipped"); continue; }
		var track = matches[2];
		var cmdli = 'SetAttr META="track:'+track+'" FILE="'+files(i).realpath+'"';
		DOpus.Output("    cmd: " + cmdli);
		cmd.RunCommand(cmdli);
	}

	for(var i=0;i<files.count;i++){
		DOpus.Output("File: [" + files(i).name+"]");
		var matches = String(files(i).name).match(re);
		if (!matches){ DOpus.Output("    skipped"); continue; }
		var title = matches[3];
		var cmdli = 'SetAttr META="title:'+title+'" FILE="'+files(i).realpath+'"';
		DOpus.Output("    cmd: " + cmdli);
		cmd.RunCommand(cmdli);
	}
}

The code works fine, but I have repeat the for loop three time for updating the three ( artist track title) field. Is it Ok or is there any easy way to write the field updater command in a single loop?

The mentioned original command already adds all tags for all selected files at once.

Hi dear Sasa thanks for your nice comment. The Original codes I found that's cancommand was adds only the track no. I have been add other two fields ( artist title ) this code in JavaScript, I just peak the Regex from your code. Now Please tell me is that there any way to combined them in a single Loop?

No, it adds ALL - artist, title and number - tags from filename as you could also see in my screenshot posted today after you asked me to check the command.

You must do something wrong.

To help others interested in the original version of the script, it's one of the scripts in this post:

(And, as Sasa says, it sets all the tags at once instead of one at a time.)

By the way, the title of the song is "Beat It", not "Just Beat It". (He does sing "just beat it" in the song, but the title is only two of those words.)

1 Like

ok thanks everyone for commenting. Here is what I want:


function OnClick(data){
	var files = data.func.sourcetab.selected_files;
	var cmd = data.func.command; cmd.ClearFiles();
	var re = new RegExp("(.*) - ([0-9]*) - (.*)(\.mp3|\.flac)");


	for(var i=0;i<files.count;i++){
	//	DOpus.Output("File: [" + files(i).name+"]");
		var matches = String(files(i).name).match(re);
	//	if (!matches){ DOpus.Output("    skipped"); continue; }
		var artist = matches[1];
		var track = matches[2];
		var title = matches[3];
		var cmdli = 'SetAttr META="artist:'+artist+'" "track:'+track+'" "title:'+title+'" FILE="'+files(i).realpath+'"';
		//DOpus.Output("    cmd: " + cmdli);
		cmd.RunCommand(cmdli);
	}
	}

need a little more help!
after the tagging part done, I want to rename the filenames also with var title = matches[3]; what I mean

Michael Jackson - 01 - Beat it.flac
should be Beat it.flac
Celine Dion - 02 - My Heart Will Go On.flac
should be My Heart Will Go On.flac
Jennifer Lopez - 03 - Waiting For Tonight (Remix).flac
should be Waiting For Tonight (Remix).flac
for that should I start a new function like function OnGetNewName(getNewNameData)
or is there any way to rename those selected files in the same function function OnClick(data)

any light's?

You can run the Rename command in a similar way to how you're already running the SetAttr command.

I have tried with this
cmd.RunCommand('Rename FROM="'+ files +'" TO="' + title + '" IGNOREEXT');
but not works

image

Compare your FILES argument in the SetAttr and Rename commands. You are passing files(i) (just one file) to one and files (a list of files) to the other.

Ok then I have tried with this

cmd.RunCommand('Rename FROM="'+ files(i) +'" TO="' + title + '" IGNOREEXT');

but it's can works for only one file. can give me a little more help!
Look Leo I have tried to solve my problem this days :smiley:

If you want to pay a consultancy fee I'm sure someone will write all the custom things you want every day. I have too much work to do to help more, sorry.

4 Likes

Thanks :expressionless:

function OnClick(data){
	var files = data.func.sourcetab.selected_files;
	var cmd = data.func.command; cmd.ClearFiles();
	var re = new RegExp("(.*) - ([0-9]*) - (.*)(\.mp3|\.flac)");


	for(var i=0;i<files.count;i++){
	//	DOpus.Output("File: [" + files(i).name+"]");
		var matches = String(files(i).name).match(re);
	//	if (!matches){ DOpus.Output("    skipped"); continue; }
		var artist = matches[1];
		var track = matches[2];
		var title = matches[3];
		var cmdli = 'SetAttr META="artist:'+artist+'" "track:'+track+'" "title:'+title+'" FILE="'+files(i).realpath+'"';
		//DOpus.Output("    cmd: " + cmdli);
		cmd.RunCommand(cmdli);
	//	cmd.RunCommand('Rename FROM="'+ files(i) +'" TO="' + title + '" IGNOREEXT');
	}

	for(var i=0;i<files.count;i++){
		var matches = String(files(i).name).match(re);
		var title	= matches[3];
cmd.RunCommand('Rename FROM="'+ files(i) +'" TO="' + title + '" IGNOREEXT');
	

	}
	}

I do what I can; I use 2nd for loop which is not the right way; If any one in this forum wish can show me the right way.