设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 服务器 > 系统 > 正文

Windows – PowerShell复制项方法失败 – 文件名中的括号

发布时间:2021-01-28 02:13 所属栏目:52 来源:网络整理
导读:我试图使用Power Shell(v.1)仅复制匹配模式的文件.文件命名约定是: Daily_Reviews[0001-0871].journalDaily_Reviews[1002-9887].journal[...] 当我运行它时,方法“Copy-Item”抱怨: 无法检索cmdlet的动态参数.指定的通配符模式无效:Daily_Reviews [0001-0

我试图使用Power Shell(v.1)仅复制匹配模式的文件.文件命名约定是:

Daily_Reviews[0001-0871].journal
Daily_Reviews[1002-9887].journal
[...]

当我运行它时,方法“Copy-Item”抱怨:

无法检索cmdlet的动态参数.指定的通配符模式无效:Daily_Reviews [0001-0871] .journal
??Copy-Item<<<< $sourcefile $destination 该错误是由文件名中的“[”和“]”引起的.当我删除左右括号时,它按预期工作.但看起来PowerShell 1没有-LiteralPath标志,那么是否有另一种方法可以让PowerShell 1中的Copy-Item工作,文件名包含括号?

$source = "C:\Users\Tom\"
$destination ="C:\Users\Tom\Processed\"

if(-not(Test-Path $destination)){mkdir $destination | out-null}


ForEach ($sourcefile In $(Get-ChildItem $source | Where-Object { $_.Name -match "Daily_Reviews\[\d\d\d\d-\d\d\d\d\].journal" }))
{

  Copy-Item $sourcefile $destination
 }

解决方法

好了,经过研究,我发现了一个解决方法:

$src = [Management.Automation.WildcardPattern]::Escape($sourcefile)
Copy-Item  $src $destination

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读