Select files that contain name similar to the selected file

As suggested by Leo, I'm creating a single post to explain what I was trying to do :slightly_smiling_face:

When I select a file by clicking on it, I want to look for files with a similar name before the first dot. To make this happen, I used the following function:

Clipboard COPYNAMES=nopaths REGEXP ([^\.]*)\..* \1 
Select PATTERN "{clip}*"

If I have a file named "test1.0", then it will select files that contain "test1" in the name, that is, files like "test1.5", "test1.0abc", "test1.aeo".

However, the function I used interacts with old names copied to the clipboard in case I decide to use the function more than once. For example: if now I decide to interact with a file named "play1.0", the function will also erroneously interact with "test1.0".

2 Likes

There's a built-in command for this:

Select SIMILARBASE
1 Like

Did not work. The first dot in the filename was deliberately placed by me, it is not the dot before the extension. For this reason, David helped me with a regular expression to capture the name of the selected file before the first point, via Clipboard COPYNAMES=nopaths REGEXP ([^\.]*)\..* \1 . Therefore, when using Select PATTERN "{clip}*", all files containing the previously captured name are selected.

For example:
test1.0.png
Capture "test1" and look for files given that name (test1.5.png, test1.0abc.png, test1.aeo.png)

Works for what was specified. The extra detail should have been in the question. :slight_smile:

This should cover what you're now asking for:

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	cmd.deselect = false; // Prevent automatic deselection
	if (clickData.func.sourcetab.selected.count == 0)
		return; // Nothing selected.
	var name = clickData.func.sourcetab.selected(0).name+"";
	var pat = name.replace(/^([^\.]+)\..*/, "$1");
	if (name == pat)
		return; // Name doesn't match the pattern.
	// EscapeString, in case the name contains pattern-matching characters.
	pat = DOpus.FSUtil.NewWild.EscapeString(pat);
	// Change .* to * if the dot is meant to be optional in the other files:
	var cmdLine = 'Select PATTERN="' + pat + '.*"';
//	DOpus.Output(cmdLine);
	cmd.RunCommand(cmdLine);
}

To use the script on a toolbar button: How to use buttons and scripts from this forum - #2 by Leo

2 Likes

The extra detail should have been in the question.

Yes, I should have been more specific.

It worked perfectly, thank you very much for your help and patience. Thanks also to the other members who helped :grin:

2 Likes

Excuse my english, and the question I'm going to ask, but what is this script really for? Well, I select files and execute the script from a button (JScript), and even if there are other files with similar names, none are selected. Thank you

Works with similarly named files before the first dot.

For example:
test1.0.png
Capture "test1 " and look for files given that name (test1.5.png, test1.0abc.png, test1.aeo.png )

1 Like

Thank you TeslaRename for the time and the explanation, now I understand. Then is essential that the names of the files contain a point, if this does not exist, no file is selected even if they contain words in common, that is why the script did not produce any change in my list of files.

This script is very interesting, but would it be very difficult to adapt it so that it would select all the files that contain any of the words of the selected file?

01

I tried to modify a few things, but I really don't know

function OnClick(clickData)
{
var cmd = clickData.func.command;
cmd.deselect = false; // Prevent automatic deselection
if (clickData.func.sourcetab.selected.count == 0)
return; // Nothing selected.
var name = clickData.func.sourcetab.selected(0).name+"";
var pat1 = name.replace(/^([^\s]+)\s([^\s]+)\s([^\s]+)(.*?)/, "$1");
var pat2 = name.replace(/^([^\s]+)\s([^\s]+)\s([^\s]+)(.*?)/, "$2");
var pat3 = name.replace(/^([^\s]+)\s([^\s]+)\s([^\s]+)(.*?)/, "$3");
if (name == pat1|pat2|pat3)
return; // Name doesn't match the pattern.
// EscapeString, in case the name contains pattern-matching characters.
pat1 = DOpus.FSUtil.NewWild.EscapeString(pat1);
pat2 = DOpus.FSUtil.NewWild.EscapeString(pat2);
pat3 = DOpus.FSUtil.NewWild.EscapeString(pat3);
// Change .* to * if the dot is meant to be optional in the other files:
var cmdLine = 'Select PATTERN="' + pat1 + '*"';
var cmdLine = 'Select PATTERN="' + pat2 + '*"';
var cmdLine = 'Select PATTERN="' + pat3 + '*"';
// DOpus.Output(cmdLine);
cmd.RunCommand(cmdLine);
}

Yes, I definitely think it is possible as long as the words are separated by a common character, a space or a dot for instance.
I think that an array could be made with the words as elements of the array.
Then just one Select Pattern command with the Pattern being a logical OR ( | ).

Good Idea !

Yes David, the words would be separated by a space. Last night I spent the night researching and wanting to learn a bit of JScript, but it's not that easy, it takes time, but I'll get it in a day, thank you very much for the instruction!

Try this.

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

    if (tab.selected_files.count == 0) return;

    cmd.RunCommand('Select PATTERN="*(' + fsu.NewWild().EscapeString(tab.selected_files(0).name_stem).replace(/ /g, '|') + ')*.*"');
}
2 Likes

Perfect lxp, eternally grateful, may God give you more and more wisdom!

I have a script working, well kind of.
I'm not proud of it. I know this is little more than a Kludged hack.
It is lacking in object elegance.

As long as the filenames contain one space character between words and do not contain whitespace at the beginning or end, this is working. It makes me laugh.

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	var tab = clickData.func.sourcetab;
	var fsu = DOpus.FSUtil();
	var i = 0;
	var j = 0;	
	cmd.deselect = false; // Prevent automatic deselection
	if (tab.selected.count == 0)
		return; // Nothing selected.
	var name = tab.selected(0).name+"";	
	var pat = name.replace(/^(.*)\.[^.]*$/, "$1");  // removes file extension.
	// EscapeString, in case the name contains pattern-matching characters.
	pat = fsu.NewWild.EscapeString(pat);
	var strl = pat.length;    
    
	var words = [];
	words[0] = "";    
	while ( i < strl )   
	     { 			  	   
		  if ( pat[i] == " " )
		       { 
			   j++;
			   words[j] = "";			   		  
			   }
		  else
		       {			    
			    words[j]= words[j] + pat[i];			
			   }
         i++;	    		   	   
	     }
    var wordpat = words.join('|');    
	var cmdLine = 'Select PATTERN="*(' + wordpat + ')*"';	// words in select pateern need not be separated by a space character.	This works.
	//var cmdLine = 'Select PATTERN="* (' + wordpat + ') *"'; // words in select pattern are separated by a space character, but doesn't work at beginning or end of filename.
    
    // DOpus.Output(cmdLine);
	cmd.RunCommand(cmdLine);
	}

Thank you very much Davi for the time spent, the script works very well, the same as the one created by lpx, but I wanted to ask you, is it possible to add a line to disable the button when there is no selected element?

Type: @disablenosel

I think it needs to be listed under the Modifiers tab.
modifier