1

EDIT: I'll leave the question here as the help docs don't mention wildcard escaping, but using [WildcardPattern]::Escape('C:\temp\file[file].txt') works.

On Windows (10) it's valid for file paths to contain square brackets []. eg:

C:\temp\file[file].txt

is valid.

However when I pass this file path to the Powershell Start-BitsTransfer command's -source parameter it either errors (if it's the only file path) or is ignored (if supplied as a string array element).

I suspect it's because of the range operation support:

You can use standard wildcard characters such as the asterisk (*) and the question mark (?). Or, you can use a range operator such as "[a-r]".

And the doc states:

Note

The destination path cannot use wildcard characters. The destination path supports relative directories, root paths, or implicit directories (that is, the current directory). Destination files cannot be renamed by using a wildcard character. Additionally, HTTP and HTTPS URLs do not work with wildcards. Wildcards are only valid for UNC paths and local directories.

However doesn't mention source not supporting wildcards only destination.

How do I pass literal file paths to Start-BitsTransfer that contain square brackets?

PS: I've also tried specifying a specific output path for a given input path containing brackets and still fails.

Geordie
  • 1,920
  • 2
  • 24
  • 34
  • 2
    the problem is that the file name itself has chars that are used by the file system for wildcards ... and the `-Source` parameter does not have the ability to tell the filesystem to ignore such the way that `-LiteralPath` does for `Get-ChildItem`. – Lee_Dailey Apr 19 '22 at 20:27
  • 1
    Yeah it's annoying they don't give you a similar flag and that the docs are incorrect as in they should mention source and destination paths. It also silently fails or give erroneous results so not great when copying large numbers of files. – Geordie Apr 19 '22 at 20:31
  • 4
    You would have to escape the wildcard expression pattern. Using the `[WildcardPattern]` type accelerator you can do just that calling on it's static `::Escape()` method: `[WildcardPattern]::Escape('C:\temp\file[file].txt')`. This will output `C:\temp\file\`[file\`].txt` – Abraham Zinala Apr 19 '22 at 20:40
  • 2
    @Geordie - if the escape method shown by AbrahamZinala fails to fix things, then you will likely need to rename the source files. [*sigh ...*] – Lee_Dailey Apr 19 '22 at 21:03
  • ```cd 'C:\Powershell\TestCSVs\' foreach($file in get-childitem){ Start-BitsTransfer -Source $file.PSPath -Destination C:\temp write-host $file.PSPath } ``` Worked for me with no errors. – another victim of the mouse Apr 21 '22 at 17:32

0 Answers0