我正在使用这个回答中的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
语句?
那么,我做错了什么,我该如何纠正它?
\*
技巧刚刚解决了大约六个问题。太棒了,谢谢! - dwwilson66-Recurse
参数时,无需使用通配符(\*
)。 - Ohad Schneider