【发布时间】:2013-09-08 02:56:11
【问题描述】:
我正在使用来自this answer 的 PowerShell 脚本进行文件复制。当我想使用过滤器包含多种文件类型时,就会出现问题。
Get-ChildItem $originalPath -filter "*.htm" | `
foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); `
New-Item -ItemType File -Path $targetFile -Force; `
Copy-Item $_.FullName -destination $targetFile }
像梦一样工作。但是,当我想使用过滤器包含多种文件类型时,就会出现问题。
Get-ChildItem $originalPath `
-filter "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*") | `
foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); `
New-Item -ItemType File -Path $targetFile -Force; `
Copy-Item $_.FullName -destination $targetFile }
给我以下错误:
Get-ChildItem : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Filter'. Specified method is not supported.
At F:\data\foo\CGM.ps1:121 char:36
+ Get-ChildItem $originalPath -filter <<<< "*.gif","*.jpg","*.xls*","*.doc*","*.pdf*","*.wav*",".ppt*" | `
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetChildItemCommand
我有各种括号迭代,没有括号,-filter,-include,将包含定义为变量(例如,$fileFilter)并且每次都得到上述错误,并且总是指向-filter之后的任何内容.
有趣的例外是当我编码-filter "*.gif,*.jpg,*.xls*,*.doc*,*.pdf*,*.wav*,*.ppt*" 时。没有错误,但我没有得到任何结果,也没有返回控制台。我怀疑我无意中用该语句编码了一个隐含的and?
那么我做错了什么,我该如何纠正呢?
【问题讨论】:
标签: powershell filter copy powershell-2.0