复制项目文件名中包含奇怪字符的文件(即[])

rsc*_*rin 2 powershell copy-item powershell-2.0

我必须复制文件名如下

  $filename = "Sport.EL#15.csv.[]"
Run Code Online (Sandbox Code Playgroud)

到另一个文件夹中。

如果我使用

  copy-item -force Sport.EL#15.csv.[] $dest_path 
Run Code Online (Sandbox Code Playgroud)

它不起作用。

因为我这样做了:

  foreach ($f in Get-ChildItem $pathfile | Where {!$_.PsIsContainer} | Where {$_.Name -match $filename}){....}
Run Code Online (Sandbox Code Playgroud)

错误是

  Bad argument to operator '-match': parsing "Sport.EL#15.csv.[]" - Unterminated [] set..
Run Code Online (Sandbox Code Playgroud)

我认为问题出在文件名中的[]。我该如何解决?

CB.*_*CB. 7

尝试使用:

copy-item -literalpath $filename $dest_path 
Run Code Online (Sandbox Code Playgroud)

评论后编辑:

要解决该 -match问题,请尝试:

 Where {$_.Name -match [regex]::escape($filename)}
Run Code Online (Sandbox Code Playgroud)

-match取一个regular expression值,您需要escape使用每个特殊字符regex language来避免此类问题。