我正在尝试使用PowerShell(v.1)仅复制匹配模式的文件。 文件命名约定是:
Daily_Reviews[0001-0871].journal Daily_Reviews[1002-9887].journal [...]
当我运行它,方法“复制项目”抱怨:
无法检索cmdlet的dynamic参数。 指定的通配符模式无效:Daily_Reviews [0001-0871] .journal
+ Copy-Item <<<< $ sourcefile $ destination
这是因为文件名中的“[”和“]”。 当我删除左侧和右侧的括号,它按预期工作。 但看起来像PowerShell 1没有-LiteralPath标志,所以还有另一种方法让Copy-Item在包含括号的文件名的PowerShell 1中工作?
$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