Replace path symbols for powershell

Initial discussion started here ( Passing file paths: FORCE quotes? - #7 by TrialUserSoFar ), but I'm thinking this should be moved into separate section.
TLDR, I'm passing paths to PowerShell, it reads paths differently in comparison to command line, there is need to adjust certain symbols.

Here is original code portion was used as base.

	for (var eSel = new Enumerator(selected); !eSel.atEnd(); eSel.moveNext())
	{
	//	if (eSel.item().is_dir) continue; -- if you want to skip dirs
		cmdLine += ' "';
		cmdLine += eSel.item().RealPath;
		cmdLine += '"';
	}

lxp user suggested that tick ` symbols needs to be added before odd symbols.

image
This will replace & symbol with `& ok fine.

But when I want to replace single quote ' with `' it goes bad, and there is quote fight.
`eSel.item().RealPath.replace(/'/g, '`' + "'");`
image
Any advice how I can tackle this mess? You can see that due one piece not being closed properly, syntax breaks.

There are definitely CallOfDuty vibes, where player almost fights into wrong direction.


Played more.

		var cleanedPath = eSel.item().RealPath
		cleanedPath = cleanedPath.replace(/&/g, '`&');
		cmdLine += ' "' + cleanedPath + '"';

Now there is an error about 2nd line : Object doesn't support this property or method (0x800a01b6)
But just minutes 30 similar replace code worked. o_O

You have two problems to solve:

  1. Working out the correct command line to run.

  2. Working out how to generate that command line from a script.

The 2nd part is easy for us to help with, but you may need to work out the 1st part yourself, unless there's a PowerShell expert reading the forum with time to help.

So, open a Command Prompt and the PowerShell docs and work out the command line that works when you type it by hand. Opus and scripts shouldn't be involved at all yet. Once you have that worked out, the rest should be easy, and we can help with the last details.

Not exactly... in PowerShell, quotes are escaped with backticks. I am not sure about other symbols. And Opus/JScript paths don't contain quotes, so there is nothing to escape there.

As Leo said, you can probably focus on the PS part and do not need to worry about the Opus side.


Based on observation.

Double quotes do work if odd symbols are in middle of paths.
But break if quote symbols occur in path.

Double quotes are not necessary, if you can insert ` symbols before "bad" symbols.
So to me, it looks like all is necessary is to perform replace for certain symbols.

While I'm trying to figure this out, any ideas why .replace method may not work?
From ChatGPT I had one temp variant, and it executed and did the thing.
But after that it simply stopped to work.

DO13.

function OnClick(clickData)
{
	var cmdLine = 'powershell D:\\_DOCUMENTS\\WORKS.Scripts\\SOURCE---Python3_make_contact-master\\make_contacts_POWERSHELL_v1_files.ps1'

	var selected = clickData.func.sourcetab.selected;
	if (selected.count == 0) return; //-- to block launching the program with no paths

	for (var eSel = new Enumerator(selected); !eSel.atEnd(); eSel.moveNext())
	{
    	var itemPath = eSel.item().Path;
	    var cleanedPath = itemPath.replace(/a/g, 'aa');
		cmdLine += ' "' + cmdLine + '"';	
	}

	var cmd = clickData.func.command;
	cmd.ClearFiles();
	cmd.RunCommand(cmdLine);
	DOpus.Output(cmdLine); //-- for testing
}

Error at line 11, position 6 line 11 is the one with replace method.
Object doesn't support this property or method (0x800a01b6)

I did found an topics where persons tried to use find() and such thing was not available in version in DO.
There were documents with specs attached, and replace() method is there.

Could someone give a hint?

eSel.item().Path is an Opus Path object, not a JScript String object, so it doesn't have a Replace method.

Easiest way to turn it into a string is eSel.item().Path + ""

1 Like

Thanks Leo, now it again works for me.

    	var itemPath = eSel.item().RealPath + "";
	    var cleanedPath = itemPath.replace(/&/g, '`&').replace(/'/g, "`'");
		cmdLine += ' "' + cleanedPath + '"';

You add more replace pieces for other symbols.

Because I commented out cmdLine += ' "'; from initial code this broke replace part.