【问题标题】:copy-item a file with strange characters in filename (i.e [])复制文件名中包含奇怪字符的文件(即 [])
【发布时间】:2013-09-25 08:36:46
【问题描述】:

我必须复制一个文件名如下

  $filename = "Sport.EL#15.csv.[]"

进入另一个文件夹。

如果我使用

  copy-item -force Sport.EL#15.csv.[] $dest_path 

它不起作用。

因为我这样做了:

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

错误是

  Bad argument to operator '-match': parsing "Sport.EL#15.csv.[]" - Unterminated [] set..

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

【问题讨论】:

    标签: powershell powershell-2.0 copy-item


    【解决方案1】:

    尝试使用:

    copy-item -literalpath $filename $dest_path 
    

    评论后编辑:

    解决-match 问题尝试:

     Where {$_.Name -match [regex]::escape($filename)}
    

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

    【讨论】:

    • @rschirin 很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2013-04-26
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多