PowerShell的 - 对一个数组LIKE [英] Powershell - LIKE against an array
问题描述
有关正在处理每一个文件,它的名字被检查,以满足该条件。例如,给定的过滤器下面的列表:
For every file being processed, its name is being checked to satisfy the condition. For example, given the following list of filters:
$excludeFiles = @"
aaa.bbb
ccccc.*
ddddd???.exe
"@ | SplitAndTrim;
如果它匹配任何的线条应该排除处理的文件。试图避免匹配/正则表达式
,因为这个脚本需要被别人修改谁不知道它,有几个地方需要它来实现。
It should exclude a file from processing if it matches any of the lines. Trying to avoid match/regex
, because this script needs to be modifiable by someone who does not know it, and there are several places where it needs to implemented.
$ excludedFiles
和类似的被定义为宗旨此处的字符串。它允许最终用户/操作员粘贴一堆文件名从一CMD / PowerShell窗口。
$excludedFiles
and similar are defined as a here-string on purpose. It allows the end user/operator to paste a bunch of file names right from the CMD/Powershell window.
看来,PowerShell不接受般
针对一个数组,所以我不能写这样的:
It appears that Powershell does not accept -like
against an array, so I cannot write like this:
"ddddd000.exe" -like @("aaa.bbb", "ccccc.*", "ddddd???.exe")
我错过了一个选择吗?如果它本身不被支持PowerShell的,什么来实现它的最简单的方法?
Did I miss an option? If it's not natively supported by Powershell, what's the easiest way to implement it?
推荐答案
您可以对名称的集合进行模式匹配,但不反对的模式列表。试试这个:
You can perform a pattern match against a collection of names, but not against a list of patterns. Try this:
foreach($pattern in $excludeFiles)
{
if($fileName -like $pattern) {break}
}
下面就是如何将它包装成一个功能:
Here is how it can be wrapped into a function:
function like($str,$patterns){
foreach($pattern in $patterns) { if($str -like $pattern) { return $true; } }
return $false;
}
这篇关于PowerShell的 - 对一个数组LIKE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!