Page 1 of 2

Automatic retrieval of tags from the filename

Posted: 27 Jan 2025 16:48
by atisoro
Hello to ALL
This forum is a must, and the XYPLORER program is a gem.
with your help I created the scripts that make it easier for me to navigate through the files.
thank you for this.
I'm asking for your support for a problem I haven't solved yet:
I'm trying to use a script that checks if one or more tags from the previously created list are found in the selected file, and if there are, to automatically add them to that file.
as an example:
+-------------------------------------
taglist("good, bad, ugly") :titter:
+-------------------------------------
myfile.txt
the ugly file.docx
a real ugly document.doc
ugly file but good.txt

the ideal result would be to add the tags to each file and delete them from the name
+---Name--------------------------------Tags--
myfile.txt
the file.docx ...... ugly
a real document.doc ...... ugly
file but.txt ..... good,ugly

any suggestion is appreciated

Re: Automatic retrieval of tags from the filename

Posted: 27 Jan 2025 17:40
by atisoro
The results so far:
script for adding tags from the name of the parent folder:

Code: Select all

tag replace(getpathcomponent(<curpath>, "component", -1), "|", ","), , 1, 1; 
credit to highend  (2012) from post viewtopic.php?p=75503#p75503

Re: Automatic retrieval of tags from the filename

Posted: 27 Jan 2025 17:50
by atisoro

Code: Select all

//=============================================================
//|    rename by tags, label, extra 1-2-3 and audio/video duration
//=============================================================
 end confirm("Start rename?", , , 4) != 1;
 foreach($file, <get "SelectedItemsPathNames" <crlf>>, <crlf>, "e") {
 	$base = gpc($file, "base");
        $ext  = gpc($file, "ext");
        $label = trim(tagitems("tags", , $file, 2).tagitems("label", , $file, 2).tagitems("ex1", , $file, 2).tagitems("ex2", , $file, 2).tagitems("ex3", , $file, 2));
	$durata = round(property("System.Media.Duration", $file) / 10000000);
	if $durata == 0 { $minute = ""; }
        elseif $durata < 60 { $minute = "($durata"." sec)"; }
        else { $minute = "(".round($durata / 60)." min)"; }
	renameitem(trim("$minute $label $base.$ext"), $file);
		}
        }

Code: Select all

//=============================================================
//|    rename file by adding folder name
//=============================================================
	rename b, "<folder> - *";

Code: Select all

//=============================================================
 / /|    undo rename by folder
//=============================================================
     foreach($file, <get "SelectedItemsPathNames" <crlf>>, <crlf>, "e") {
     $base = gpc($file, "base");
     renameitem(replace($base, <folder>." - "), $file);
     }

Re: Automatic retrieval of tags from the filename

Posted: 27 Jan 2025 18:15
by highend

Code: Select all

    $tags = taglist(); // E.g.: "good, bad, ugly"
    end (!$tags), "No taglist item(s), aborted!";

    $sel = <get SelectedItemsPathNames>;
    end (!$sel), "No item(s) selected, aborted!";

    $tags = replace($tags, ", ", "|");
    foreach($file, $sel, <crlf>, "e") {
        $base  = gpc($file, "base");
        $match = regexmatches($base, "\b(" . $tags . ")(\b|$)");
        if ($match) {
            $newTags = replace($match, "|", ",");
            tagitems("tags", $newTags, $file);
            $newBase = regexreplace($base, "\b(" . $match . ")(\b|$)");
            $newBase = trim(regexreplace($newBase, "[ ]{2,}", " "));
            renameitem($newBase, $file);
        }
    }

Re: Automatic retrieval of tags from the filename

Posted: 27 Jan 2025 18:56
by atisoro
highend you are the wizard of scripts.
it's perfect!
and very fast!
clean code like a diamond.
thousands of thanks :appl:

is it possible to make 2 small additions?
1. to take the current list of tags (which appears in the quick menu Add tags by list...)

and

2. to avoid errors that the file already exists by adding a numerical sequence or similar
got error

Code: Select all

Rename target exists already: C:\Users\atisoro\Desktop\Fld\neutral\text.txt

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 10:09
by highend
1. What's wrong with $tags = taglist(); here?

2. renameitem($newBase, $file, 16);

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 16:41
by atisoro
Sorry for the incomplete expression of my request
Actually, taglist("a, b, c") does the job, but I would have liked to be able to select from multiple taglists, adapted for multiple scenarios.
Now I use a User button from which I choose:
  • "public list" taglist("todo, undo, redo"); //default
    "office list" taglist("verified, approved, rejected");
    "test" taglist("good, bad, ugly");
and several other lists.
It would be very helpful to adapt the script in this regard.

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 17:02
by atisoro

Code: Select all

renameitem($newBase, $file, 16);
yes, I was careless and didn't check the help file.
It's very clear now:

Code: Select all

flags 0: [Default] Smart (keep extension unless extension is passed)
1: Rename base (keep extension)
2: Rename extension (keep base)
[1 + 2 = 3: Rename all (base and extension)]
4: Show dialog (Suffix / Overwrite) on name collision.
8: Silent overwrite on name collision.
16: [b]Force appending the numsuffix[/b]. If numsuffix is not passed as 4th argument then it defaults to the incremental suffix set in Configuration.

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 18:34
by highend
Then define them e.g. in a heredoc and choose one via inputselect?

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 19:18
by atisoro

Code: Select all

heredoc
?
Can you show me an example?

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 19:26
by highend

Code: Select all

    $tags = <<<>>>
a|b|c
d|e|f
g|h|i|j|k
    >>>;

    $tags = inputselect("Choose tag(s)", $tags, <crlf>, 4+32+8192+16384, , 600, 400);
?

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 19:30
by atisoro
YES, thank you!

Re: Automatic retrieval of tags from the filename

Posted: 28 Jan 2025 19:38
by atisoro
Working code - highend masterpiece

Code: Select all

    $tags = <<<>>>
a|b|c
d|e|f
g|h|i|j|k
    >>>;

    $tags = inputselect("Choose tag(s)", $tags, <crlf>, 4+32+8192+16384, , 600, 400);

    $sel = <get SelectedItemsPathNames>;
    end (!$sel), "No item(s) selected, aborted!";

    foreach($file, $sel, <crlf>, "e") {
        $base  = gpc($file, "base");
        $match = regexmatches($base, "\b(" . $tags . ")(\b|$)");
        if ($match) {
            $newTags = replace($match, "|", ",");
            tagitems("tags", $newTags, $file);
            $newBase = regexreplace($base, "\b(" . $match . ")(\b|$)");
            $newBase = trim(regexreplace($newBase, "[ ]{2,}", " "));
            renameitem($newBase, $file, 16);
        }
    }
problem solved for me

Re: Automatic retrieval of tags from the filename

Posted: 30 Jan 2025 20:12
by atisoro
Actually, it would be a small improvement...
this line

Code: Select all

$allScripts = report("{basename} - ({comment})|",$allScripts);
generates a wrong result for example my file - ()
(this happend only was no comment)
and it would be cleaner if there were no comments to include only basename without minus sign and brackets.
and if there are comments it remains {basename} - ({comment})

Re: Automatic retrieval of tags from the filename

Posted: 30 Jan 2025 20:17
by highend
What?