Copy file with square brackets [ ] in the filename and use * wildcard

后端 未结 9 1417
迷失自我
迷失自我 2020-12-08 22:35

I\'m using PowerShell on Windows 7, and writing a script to copy a bunch of files from one folder structure to another. Kind of like compiling. The PowerShell Copy-I

相关标签:
9条回答
  • 2020-12-08 23:11

    There's a difference between ' and `:

    • The first is the single quote that is the non-shift character on the " key.
    • The second is the backtick that I thought I was using but actually wasn't. It's the nonshift character on the ~ key.

    This works:

    # to Fusion Server
    Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\'``[RoomView``] Versions explained*.pdf' -Destination $FSG\$containerFolder\$rootFolder\"Fusion Server"\
    
    0 讨论(0)
  • 2020-12-08 23:13

    On PowerShell v 2.0 and up the escape character to use is the backslash. For example, if we want to remove the brackets from this string "[Servername: QA01]" which is the sort of output we get from the Exchange Admin PowerShell cmdlet activity in System Center Orchestrator, we use the following logic:

    $string -replace '\[','' -replace '\]',''
    >Servername: QA01
    

    This is pretty weird. See, you have to use a single-quote (which normally implies in PowerShell 'evaluate this precisely as written', so this is very odd syntax).

    Don't feel bad for not figuring this out on your own, this is very odd syntax.

    0 讨论(0)
  • 2020-12-08 23:15

    Apparently, square brackets need double-backticks to escape, which is unusual. Reference here.

    You're sure that doesn't work? I've seen it referred to a few times.

    Edit: Yes, it works, you used double quotes instead of backticks.

    Double quote is above the apostrophe character, next to the Enter key. Backtick is right underneath the Escape key, sharing the key with the tilde, ~.

    0 讨论(0)
提交回复
热议问题