Text Replace for folder path copied

Hi, Dears,
I am asking for a quick help: I want to achieve that maybe I can set up a new button in DOpus and it'll copy the current file/folder's full path, but replace my username part into a general approach, i.e. change "C:\Users\bryan" into "%userprofile%".

I have already searched in here, and got below page really alike my need.
https://resource.dopus.com/t/replace-text-in-clipboard/9969
However my ability of RegExp is really not enough for me.
So far I tried

Clipboard COPYNAMES=unc REGEXP "^C:\Users\bryan(.)*$" "%userprofile%\2"

It doesn't work.
Can anybody help, please.

thank you.

Try

@noexpandenv 
Clipboard COPYNAMES=unc REGEXP "C:\\Users\\bryan(.*)" "%userprofile%\1"

Super! works like a charm.
Thanks a lot.

Hello, I hope I can ask by attaching to this solved question:

if I copy the OneDrive Path, natively from Windows it'll be:

C:\Users\huangb\COMPANY NAME\Teams Folder - Documents\....

@lxp gave a perfect help on the issue of replacing my username into non-specific one.
with his script, I can end up like:

%Userprofile%\COMPANY NAME\Teams Folder - Documents\...

But then I found further trouble:
I am in English language, and my colleagues in German environment got this in their local folder:

%Userprofile%\COMPANY NAME\Teams Folder - Documen**te**\...

Annoying!

My question now, can I get the script into:

EN:
%Userprofile%\COMPANY NAME\Teams Folder - Documen**ts**\...
DE:
%Userprofile%\COMPANY NAME\Teams Folder - Documen**te**\...

with two rows automatically. I am struggling with it without success.

Thanks.

If you turn off Preferences / Folders / Folder Display / Display localized folder names, you'll probably see the folder's real name, which is probably the same in all languages.

Putting more than one line per file into the clipboard is probably too much for the Clipboard command. A little script can help:

// https://resource.dopus.com/t/text-replace-for-folder-path-copied/40055

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

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

    var tmp = '';
    for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var stdPath = String(item.realpath).replace(/^C:\\User\\huangb/, '%userprofile%');
        tmp += 'EN:\r\n';
        tmp += stdPath + '\r\n';
        tmp += 'DE:\r\n';
        tmp += stdPath.replace(/Documents/, 'Documente') + '\r\n';
        tmp += '\r\n';
    }

    DOpus.SetClip(tmp);

    // cmd.RunCommand('Set UTILITY=otherlog');
    // DOpus.ClearOutput();
    // DOpus.Output(tmp);
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>Text Replace For Folder Path Copied</label>
	<icon1>#clipcopy</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/text-replace-for-folder-path-copied/40055</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>    if (tab.selected_dirs.count == 0) return;</instruction>
		<instruction />
		<instruction>    var tmp = &apos;&apos;;</instruction>
		<instruction>    for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        var stdPath = String(item.realpath).replace(/^C:\\User\\huangb/, &apos;%userprofile%&apos;);</instruction>
		<instruction>        tmp += &apos;EN:\r\n&apos;;</instruction>
		<instruction>        tmp += stdPath + &apos;\r\n&apos;;</instruction>
		<instruction>        tmp += &apos;DE:\r\n&apos;;</instruction>
		<instruction>        tmp += stdPath.replace(/Documents/, &apos;Documente&apos;) + &apos;\r\n&apos;;</instruction>
		<instruction>        tmp += &apos;\r\n&apos;;</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    DOpus.SetClip(tmp);</instruction>
		<instruction />
		<instruction>    // cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction>    // DOpus.ClearOutput();</instruction>
		<instruction>    // DOpus.Output(tmp);</instruction>
		<instruction>}</instruction>
	</function>
</button>

1 Like

Hey, Leo,
thanks for reply.
Yes I've thought about it, but I think this is generated automatically when I click SharePoint "Sync to Local" Button, i.e. not relavent to the "Localized folder name" topic. -- But obivously the same philosophy from Microsoft was there.

Again works great!
minor issue exist in

 var stdPath = String(item.realpath).replace(/^C:\\User\\huangb/, '%userprofile%');       

the replacing wasn't successful.
But I'll take this as my home work, in case I fail eventually I'll come to you again.
Big Thanks!

Yes, there seems to be an s missing.

1 Like

Dear Lxp,
Unfortunatenly my further mod failed based on your work.
I want to set up an "if logic" to only do the EN/DE variant when there is "Documents" showing up in the path. So I tried below (with googling JScript "Includes" event).


// https://resource.dopus.com/t/text-replace-for-folder-path-copied/40055

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

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

    var tmp = '';
	
        for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
            var item = e.item();
            var stdPath = String(item.realpath).replace(/^C:\\Users\\huangb/, '%userprofile%');		
			DOpus.output(stdPath)
			var result=String(stdPath).includes('Documents');	
			
            if(result){
            tmp += 'EN:\r\n';
            tmp += stdPath + '\r\n';
            tmp += 'DE:\r\n';
            tmp += stdPath.replace(/Documents/, 'Dokumente') + '\r\n';
            tmp += '\r\n';     
	        }else{
		 		tmp += stdPath
			}
   		 }

    DOpus.SetClip(tmp);

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

Could you kindly again help, please?
Much appreciated!

Untested, but I think this should work:

// https://resource.dopus.com/t/text-replace-for-folder-path-copied/40055

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

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

    var tmp = '';

    for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var stdPath = String(item.realpath).replace(/^C:\\Users\\huangb/, '%userprofile%');
        var altPath = stdPath.replace(/Documents/, 'Dokumente');
        // DOpus.output(stdPath)

        if(stdPath != altPath) {
            tmp += 'EN:\r\n';
            tmp += stdPath + '\r\n';
            tmp += 'DE:\r\n';
            tmp += altPath;
            tmp += '\r\n\r\n';
        } else {
            tmp += stdPath
            tmp += '\r\n';     
        }
    }

    DOpus.SetClip(tmp);
}
2 Likes

I am afraid JScript doesn't support includes(). You could use indexOf() instead, but Leo's solution should be just fine.

1 Like

indeed! Thank you!

Worked.
big thanks!

1 Like

:sweat_smile:
Sorry for coming back again:
I came to realize that the script only works for "folder/dir", but not "files".
I think it's coming from

new Enumerator(tab.selected_dirs)

but if I swtich to tab.selected_files, it still doesn't work.

How can we fix this please?

selected will give you both files and folders, if both is what you want.

1 Like

Hi, Leo,
yes I want both and yes it works.
Thanks again!