如何正确地过滤PowerShell复制脚本中的多个字符串 [英] How to properly -filter multiple strings in a PowerShell copy script

查看:776
本文介绍了如何正确地过滤PowerShell复制脚本中的多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是来自此答案的PowerShell脚本< a>做一个文件复制。当我想使用过滤器包括多个文件类型时出现问题。

I am using the PowerShell script from this answer to do a file copy. The problem arises when I want to include multiple file types using the filter.

Get-ChildItem $originalPath -filter "*.htm"  | `
   foreach{ $targetFile = $htmPath + $_.FullName.SubString($originalPath.Length); ` 
 New-Item -ItemType File -Path $targetFile -Force;  `
 Copy-Item $_.FullName -destination $targetFile }

。但是,当我想使用过滤器包括多个文件类型时出现问题。

works like a dream. However, The problem arises when I want to include multiple file types using the filter.

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 / code>,定义包含的变量(例如 $ fileFilter ),每次得到上面的错误,总是指向后面 -filter 。

I have various iterations of parentheses, no parentheses, -filter, -include, defining the inclusions as variable (e.g., $fileFilter) and each time get the above error, and always pointing to whatever follows -filter.

有趣的例外是当我编写 -filter* .gif,*。 jpg,*。xls *,*。doc *,*。pdf *,*。wav *,*。ppt *。没有错误,但我没有得到任何结果,没有回到控制台。我怀疑我不经意地用该语句编写了一个不明确的

The interesting exception to that is when I code -filter "*.gif,*.jpg,*.xls*,*.doc*,*.pdf*,*.wav*,*.ppt*". There are no errors, but I and get no results and nothing back to the console. I suspect I've inadvertently coded an impicit and with that statement?

所以我做错了,如何纠正它?

So what am I doing wrong, and how can I correct it?

推荐答案

-Filter 只接受一个字符串。 - 包括接受多个值,但符合 -Path 参数。诀窍是将 \ * 附加到路径末尾,然后使用 -Include 选择多个扩展名。 BTW,引用字符串在cmdlet参数中是不必要的,除非它们包含空格或shell特殊字符。

-Filter only accepts a single string. -Include accepts multiple values, but qualifies the -Path argument. The trick is to append \* to the end of the path, and then use -Include to select multiple extensions. BTW, quoting strings is unnecessary in cmdlet arguments unless they contain spaces or shell special characters.

Get-ChildItem $originalPath\* -Include *.gif, *.jpg, *.xls*, *.doc*, *.pdf*, *.wav*, .ppt*

请注意,无论 $ originalPath 是否以反斜杠结尾,这都将起作用,因为多个连续的反斜杠会被解释为单个路径分隔符。例如,尝试:

Note that this will work regardless of whether $originalPath ends in a backslash, because multiple consecutive backslashes are interpreted as a single path separator. For example, try:

Get-ChildItem C:\\\\\Windows

这篇关于如何正确地过滤PowerShell复制脚本中的多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆