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

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

问题描述

我正在使用 这个答案中的 PowerShell 脚本 进行文件复制.当我想使用过滤器包含多种文件类型时,就会出现问题.

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:datafooCGM.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 后面的内容.

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 只接受单个字符串.-Include 接受多个值,但限定 -Path 参数.诀窍是将 * 附加到路径的末尾,然后使用 -Include 来选择多个扩展名.顺便说一句,在 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天全站免登陆