Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

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")

Did I miss an option? If it's not natively supported by Powershell, what's the easiest way to implement it?

like image 918
Neolisk Avatar asked Oct 22 '12 20:10

Neolisk


1 Answers

Here is a short version of the pattern in the accepted answer:

($your_array | %{"your_string" -like $_}) -contains $true

Applied to the case in the OP one obtains

PS C:\> ("aaa.bbb", "ccccc.*", "ddddd???.exe" | %{"ddddd000.exe" -like $_}) -contains $true
True
like image 135
davidhigh Avatar answered Oct 19 '22 05:10

davidhigh