2

The point of this odd little script is just to let me choose a directory and have whatever is in my clipboard written to a text file in that path. This path will unfortunately always contain parentheses, brackets, and sometimes braces.

Y:

Function Get-Folder($initialDirectory="")

{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.rootfolder = "MyComputer"
    $foldername.SelectedPath = $initialDirectory

    if($foldername.ShowDialog() -eq "OK")
    {
        $folder += $foldername.SelectedPath
    }
    return $folder
}

$myFolder = Get-Folder('Y:\MyStuff (OtherStuff)')

Get-Clipboard > $myFolder\myFile.txt

Because of the brackets in the subfolder, I get this error message

out-file : Cannot perform operation because the wildcard path Y:\MyStuff
(OtherStuff)\Sub1\Sub2 [Info] [Some More Info]\myFile.txt did not resolve to a file.
At line:22 char:1
+ Get-Clipboard > $myFolder\myFile.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (Y:\Music (MyStu...nfo]\myFile.txt:Stri 
   ng) [Out-File], FileNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutF 
   ileCommand

No amount of -replace combinations to account for the escape characters using ' or \ has resulted in success. How can I format the string so it resolves to a real directory?

Second question Is there a way to select a path with the more user-friendly FileBrowserDialog instead of FolderBrowser where you can't type or paste in a path?

1
  • As an aside: In PowerShell, functions are invoked like shell commands - Get-Folder Y:\MyStuff (OtherStuff)' - not like C# methods - Get-Folder('Y:\MyStuff (OtherStuff)'); see Get-Help about_Parsing. If you use , to separate arguments, you'll construct an array that a function sees as a single argument. To prevent accidental use of method syntax, use `Set-StrictMode -Version 2 or higher, but note that this places additional constraints on your code. Commented Oct 26, 2020 at 1:20

2 Answers 2

2

Regrettably, > $myFolder\myFile.txt is the equivalent of
| Out-File -FilePath $myFolder\myFile.txt, and the -FilePath parameter[1] interprets its argument as a wildcard expression, in which [ and ] have special meaning.

The workaround is to use the Out-File cmdlet - or with text input, the Set-Content cmdlet - with the -LiteralPath parameter, which uses its argument(s) literally (verbatim):

Get-Clipboard | Out-File -LiteralPath $myFolder\myFile.txt

Note: In Windows PowerShell, Out-File and Set-Content use different default character encodings; in PowerShell [Core] v6+, BOM-less UTF-8 is consistently used - see this answer.

To specify the desired encoding explicitly, use the -Encoding parameter.


[1] Note: In other cmdlets the equivalent parameter is named just -Path; in PowerShell [Core] v6+, this inconsistency was corrected, and Out-File there supports both -FilePath and -Path interchangeably.

Sign up to request clarification or add additional context in comments.

Comments

1

This function may answer question 2. It will even let you choose a directory inside a zip folder. Optional InitialDirectory and message.

function Get-Folder {
    Param(
        $initialDirectory = 17,
        $message = "Choose a directory"
    )

    $shell = New-Object -ComObject Shell.Application
    $selection = $shell.browseforfolder(0,$message,65556,$initialDirectory)
    if($selection){$selection.self.path}
}

get-folder 'c:\some\pa(th)'

2 Comments

Nice; the method is documented here, and the constants for the initial dir. here.
Thanks. I also found these useful 1 2 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.