How to use regular expression with command

lxp create a button command

@nodeselect
@sync:dopusrt /cmd SetAttr FILE={filepath} META "album:{filepath|..|noterm|nopath}"

This button can copy parent folder full name in album metadata of flac file. But I Need a part of the parent folder name. so I need to use a regular expression for do this.

/(^.*?)\..*/gm

This Regexp can capture my wanted part from the folder name. Now How to add this regexp code with the button command code?

You've probably already guessed it: you'll need a script.

// https://resource.dopus.com/t/how-to-use-regular-expression-with-command/44906

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

    cmd.RunCommand('Set UTILITY=otherlog');
    // DOpus.ClearOutput();

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();

        var cmdLine = 'SetAttr' +
            ' FILE="' + item + '"' +
            ' META "album:' + item.path.filepart.replace(/(.*?)\..*/, '$1') + '"';

        DOpus.Output(cmdLine);
        // cmd.RunCommand(cmdLine);
    }
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>44906</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/how-to-use-regular-expression-with-command/44906</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction>    // DOpus.ClearOutput();</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction />
		<instruction>        var cmdLine = &apos;SetAttr&apos; +</instruction>
		<instruction>            &apos; FILE=&quot;&apos; + item + &apos;&quot;&apos; +</instruction>
		<instruction>            &apos; META &quot;album:&apos; + item.path.filepart.replace(/(.*?)\..*/, &apos;$1&apos;) + &apos;&quot;&apos;;</instruction>
		<instruction />
		<instruction>        DOpus.Output(cmdLine);</instruction>
		<instruction>        // cmd.RunCommand(cmdLine);</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>
1 Like

This Script doesn't Update the Album Metadata field of Selected flac files. This Script doesn't show any error. in script log file it's show my names part perfectly that means the regular expression works fine.

but only the problem is its doesn't update the Album Metadata field

You've probably already guessed it: you'll need to uncomment the last line in the script (now that you know the script does what you want it to do) :wink:

1 Like

:grin: :grin: :grin: Yes I have already find out the solution. This is the way you tech me a lot. Thank you so much dear brother.