Request for help with Batch Functions/ Scripts

I have used the MS-DOS Batch function shown below, and it works when I am adding Tags to just a few photos or photos in one folder:

I just tested it on over 20,000 photos in about 200 folders, and tags were added to about 25% of the photos.
I opened the folder in Flat No Folders View, selected the files, then selected the command.
Should I be using a Script Functions instead? If so, can someone please help me create one?

My first thought is that I don't understand why this is being run as as an MS-DOS Batch Function.
The @nodeselect is fine, but I don't understand why @sync: dopusrt/cmd is needed at all.

As a first step, I would try to make a new button that is a Standard Function ( Opus or External )
Then try this.
SetAttr File={filepath} ...

I'm not sure about the rest of it yet.

This should work better:

  • Folder_Name_To_Tag.ouc (1.8 KB)

  • Rename or delete the old Folder_Name_To_Tag user-command first.

  • Use the "Import" button in Customize > User Commands and point it to the downloaded .ouc file.

  • Please test on some disposable files first, in case I made a mistake. I've only done very quick testing.

Script code for reference:

I don't understand either. Many of the commands I use are ones I found in this forum and then edited slightly. I have done a lot of research on scripting/coding but am still in the dark! Thank you for offering assistance.

Thank you very much Leo,
I tested it on a test batch of about 16,000 photos. Keywords based on the folder name were added to almost 14,000 of the 16,000 before Opus crashed. Despite the crash, that's big improvement.
I just emailed the crash log.

This might work better with large numbers of files:

function OnClick(clickData)
	var cmd = clickData.func.command;

	var col = clickData.func.sourcetab.selected_files;

	if (col.count > 0)
		for (var eSel = new Enumerator(col); !eSel.atEnd(); eSel.moveNext())
			var item = eSel.item();
			var name = item.path.stem;

			if (name && name != "")
				var line = 'SetAttr FILE="' + item.RealPath + '" META "tags:+' + name + '"';
			//	DOpus.Output(line);

Works perfectly, thank you very much.
If I wanted to create another to write the filename to Tags, I suppose I would change item.RealPath, is that correct? What would it need to be changed to?