Run PowerShell script with spaces in script path & args?

Problem

I created a toolbar button to run a script. However, I got the following error:

The string is missing the terminator: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

The problem was that quotation marks and double quotes were processed by Directory Opus and erased before being passed to PowerShell.

Measures

  • Double quotes cannot be used in file names, so enclose the file path in double quotes.
  • Escape with \ to prevent double quotes from being removed by Directory Opus.

Example

powershell -noexit -Command . \"%WIN_SETUP_ABYSS%\scripts\disc\Rename-Prefix.ps1\" -LiteralPath \"{filepath}\" -DateString (Get-Clipboard -Raw)

Opus doesn't remove explicit quotes, so that shouldn't be what was happening.

Needing to escape the quotes may be an issue with powershell, but isn't something that Opus is doing.

Edit: Re-read and realise the issue was the quotes needed escaping, not that they needed adding or were being removed. That's a powershell thing, no an Opus thing, I think.


Most of what I said in the older reply isn't important, although I'll append it here in case it's useful general info for someone else:

You need to explicit quotes around environment variables that contain spaces (if they're being used in paths), since quotes are needed around paths containing spaces, and nothing adds quotes automatically around env-var expansions.

That is true whether you run the thing via Opus or via a Command Prompt or anything else.

You do not need the quotes around Opus codes like {filepath} (unless you've also suppressed automatic quoting using @nofilenamequoting) since Opus will add those for you if there's a space in the path. (But if you need the quotes to be escaped, you would indeed need to do that explicitly.)

I'm sorry. I made a mistake.
This issue has not been resolved yet. If I try to run the script by selecting a file that contains quotation marks in the path, I will get an error.

Try adding @nofilenamequoting on a new line at the top of the script.

Where is the top of the script? I tried but it didn't work.

Click the button to the left of OK ("Advanced" in English; the one with the "(V)" hotkey in your screenshot) to put the button editor into multi-line mode.

It worked. Thank you!
Finally, I set it as follows.

@nofilenamequoting
powershell -noexit -Command . \"%WIN_SETUP_ABYSS%\scripts\disc\Rename-Prefix.ps1\" -LiteralPath \"{filepath}\" -DateString (Get-Clipboard -Raw)

1 Like

There was also a problem with other special characters. See the following links for more information on the issue.
GitHub

I want you to be able to handle these characters even if they are included in the file name.

Seems more of a PowerShell issue than an Opus one.

(I'm not sure how they designed something so recently and made it so bad at basic command line parsing.)

1 Like