Use the cmd object to add items and SELECT ALL added items, but the result are all selected

Load the full script, select a few items, go to the upper level, run the command GotoPre, the SELECT ALL command does not select the added items, but the entire list.
Even if I use a new cmd object after going to the new path, it doesn't work. The log outputs the number of items added.

	    if (Script.Vars.Exists("prePath")) {
		    var prePath = Script.Vars.Get('prePath');
			cmd.RunCommand('Go "' + prePath + '"');
	        if (selected.count) {
			    var cmd2 = DOpus.Create.Command;
	            cmd2.ClearFiles();
		        for (var e = new Enumerator(selected); !e.atEnd(); e.moveNext())
		        {
		             cmd2.AddFile(e.item())
		        }
	        }
			if (cmd2.filecount) cmd2.AddLine('SELECT ALL')
			DOpus.Output("Files count: " + cmd2.filecount)
			cmd2.Run;
		}
FullScript
// Go to Previous Location
// (c) 2023 Ken

// This is a script for Directory Opus.
// See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.



// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "Go to Previous Location";
	initData.version = "v1.0(2023.10.12)";
	initData.copyright = "(c) 2023 Ken";
//	initData.url = "https://resource.dopus.com/c/buttons-scripts/16";
	initData.desc = "Jump to the previous location.";
	initData.default_enable = true;
	initData.min_version = "13.0";


}

// Called to add commands to Opus
function OnAddCommands(addCmdData)
{
	var cmd = addCmdData.AddCommand();
	cmd.name = "GotoPre";
	cmd.method = "OnGotoPre";
	cmd.desc = "";
	cmd.label = "GotoPre";
	cmd.template = "";
	cmd.hide = false;
	cmd.icon = "script";
}

// Called when a tab is activated
function OnActivateTab(activateTabData)
{
    var tab = activateTabData.oldtab;
    Script.Vars.Set("preTab", tab);
	Script.Vars.Set("prePath", tab.path);
	if (tab.format.view)
	    Script.Vars.Set("selected", tab.selected)
}

// Called before a new folder is read in a tab
function OnBeforeFolderChange(beforeFolderChange)
{
    var tab = beforeFolderChange.tab;
    Script.Vars.Set("prePath", tab.path);
	Script.Vars.Set("selected", tab.selected)
	if (Script.Vars.Exists("preTab"))
	    Script.Vars.Delete("preTab")
}

// Implement the GotoPre command
function OnGotoPre(scriptCmdData)
{
    var tab = scriptCmdData.func.sourcetab;
	var cmd = scriptCmdData.func.command;
	var selected = Script.Vars.Get("selected");
	if (selected.count) {
	    cmd.ClearFiles();
		//cmd.SetFiles(selected)
		for (var e = new Enumerator(selected); !e.atEnd(); e.moveNext())
		{
		    cmd.AddFile(e.item())
		}
	}

	if (!Script.Vars.Exists("preTab")) {
	    if (Script.Vars.Exists("prePath")) {
		    var prePath = Script.Vars.Get('prePath');
			cmd.RunCommand('Go "' + prePath + '"');
	        if (selected.count) {
			    var cmd2 = DOpus.Create.Command;
	            cmd2.ClearFiles();
		        for (var e = new Enumerator(selected); !e.atEnd(); e.moveNext())
		        {
		             cmd2.AddFile(e.item())
		        }
	        }
			if (cmd2.filecount) cmd2.AddLine('SELECT ALL')
			DOpus.Output("Files count: " + cmd2.filecount)
			cmd2.Run;
		}
		return
	}
	
	var preTab = Number(Script.Vars.Get("preTab"));
	var tabs = tab.lister.tabs;
	for (var i = 0; i < tabs.count; i++)
	{
	    if (tabs[i] == preTab) {
            cmd.RunCommand('Go TABSELECT=' + i);
			return
		}
	}
	cmd.RunCommand('Go TABUNDOCLOSE');
}

Select ALL always selects everything.

If you want to select the items added to the Command object, use:
Select FROMSCRIPT

Focusing the item will cause Opus to crash.

    var preFocusItem = Script.Vars.Get("preFocusItem");
	if (!Script.Vars.Exists("preTab")) {
	    if (Script.Vars.Exists("prePath")) {
		    var prePath = Script.Vars.Get('prePath');
			cmd.RunCommand('Go "' + prePath + '"');
			if (cmd.filecount) {
			    cmd.RunCommand('Select FROMSCRIPT')
			}
			cmd.ClearFiles();
			cmd.AddFile(preFocusItem);
			cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
		}
		return
	}

Found the solution.

	if (tab.path != "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
	    Script.Vars.Set("preFocusItem", tab.GetFocusItem);

Which item in which folder?

Please submit the crashes if they were captured. (Help > Submit Crash Logs, if using updated toolbars, or Help CRASHLOGS to run it manually.)

When the event script runs Script.Vars.Set("preFocusItem", tab.GetFocusItem); in a tab with no items, it crashes. It's a problem with the GetFocusItem object.
To make it work:

	if (tab.all.count)
	    Script.Vars.Set("preFocusItem", tab.GetFocusItem);
	else
	    Script.Vars.Set("preFocusItem", false);

Any crash logs?

I don't know where the logs are yet, but you can reproduce it, it also happens on another machine with v12.

Summary
// Go to Previous Location
// (c) 2023 Ken

// This is a script for Directory Opus.
// See https://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.



// Called by Directory Opus to initialize the script
function OnInit(initData)
{
	initData.name = "Go to Previous Location";
	initData.version = "v1.0(2023.10.12)";
	initData.copyright = "(c) 2023 Ken";
//	initData.url = "https://resource.dopus.com/c/buttons-scripts/16";
	initData.desc = "Jump to the previous location.";
	initData.default_enable = true;
	initData.min_version = "12.0";


}

// Called to add commands to Opus
function OnAddCommands(addCmdData)
{
	var cmd = addCmdData.AddCommand();
	cmd.name = "GotoPre";
	cmd.method = "OnGotoPre";
	cmd.desc = "";
	cmd.label = "GotoPre";
	cmd.template = "";
	cmd.hide = false;
	cmd.icon = "script";
}

// Called when a tab is activated
function OnActivateTab(activateTabData)
{
    var tab = activateTabData.oldtab;
    Script.Vars.Set("preTab", tab);
	Script.Vars.Set("prePath", tab.path);
/*	
	if (tab.all.count)
	    Script.Vars.Set("preFocusItem", tab.GetFocusItem);
	else
	    Script.Vars.Set("preFocusItem", false);
*/
    Script.Vars.Set("preFocusItem", tab.GetFocusItem);
}

// Called before a new folder is read in a tab
function OnBeforeFolderChange(beforeFolderChange)
{
    var tab = beforeFolderChange.tab;
    Script.Vars.Set("prePath", tab.path);
	Script.Vars.Set("selected", tab.selected);
/*
	if (tab.all.count)
	    Script.Vars.Set("preFocusItem", tab.GetFocusItem);
	else
	    Script.Vars.Set("preFocusItem", false);
*/
    Script.Vars.Set("preFocusItem", tab.GetFocusItem);

	if (Script.Vars.Exists("preTab"))
	    Script.Vars.Delete("preTab");
}

// Implement the GotoPre command
function OnGotoPre(scriptCmdData)
{
    var tab = scriptCmdData.func.sourcetab;
	var cmd = scriptCmdData.func.command;
	
	cmd.ClearFiles();
	
	if (!Script.Vars.Exists("preTab")) {
	    if (Script.Vars.Exists("prePath")) {
			var prePath = Script.Vars.Get('prePath');
			var selected = Script.Vars.Get("selected");
			var preFocusItem = Script.Vars.Get("preFocusItem");
			cmd.RunCommand('Go "' + prePath + '"');
			var focItemSel = 0, eItem;
	        if (selected.count) {
		        //cmd.SetFiles(selected);
		        for (var e = new Enumerator(selected); !e.atEnd(); e.moveNext())
		        {
					eItem = e.item();
		            cmd.AddFile(eItem);
					if (eItem + "" == preFocusItem) focItemSel = 1 
		        }
	        }
			if (cmd.filecount)
			    cmd.RunCommand('Select FROMSCRIPT');
			
			if (preFocusItem) {
			    cmd.ClearFiles();
			    cmd.AddFile(preFocusItem);
			    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
				if (!focItemSel)
				    cmd.RunCommand('Select NONE');
			}
		}
		return
	}
	
	var preTab = Number(Script.Vars.Get("preTab"));
	var tabs = tab.lister.tabs;
	for (var i = 0; i < tabs.count; i++)
	{
	    if (tabs(i) == preTab) {
            //cmd.RunCommand('Go TABSELECT=' + i);
			cmd.RunCommand("Go TABSELECT=$" + preTab);
			return
		}
	}
	cmd.RunCommand('Go TABUNDOCLOSE');
}

If you're using Opus 13, I said above how to submit the logs.

For Opus 12, see Automatic crash logs (for bug reports)

It's much better if we have those in case we need them, before we start trying to reproduce the issue, else it will potentially waste time when we get to this. There are a lot of threads to get through right now.

I didn't find that menu item, I found the output item in the specified directory.

I gave the instruction the menu items run as well, in case you didn't have it on your menus.

But you can also get the menu item from Customize / Default Toolbars if you need to update your toolbars.

There's also a list of changes to the default toolbars which can be used to update old toolbars, if you don't want to reset them entirely: Default Toolbars & Menus [Directory Opus Manual]