10

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?

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • 1
    See answer from Josh Einstein: http://stackoverflow.com/questions/848859/use-notlike-to-filter-out-multiple-strings-in-powershell – dugas Oct 22 '12 at 22:35
  • 1
    Thanks, @dugas, but I don't see why those complications - compare to the two lines below - see my accepted answer. – Victor Zakharov Oct 23 '12 at 01:18

4 Answers4

14

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
davidhigh
  • 14,652
  • 2
  • 44
  • 75
10

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;
}
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
3

I suppose you could use the Get-ChildItem -Exclude parameter:

Get-ChildItem $theFileToCheck -exclude $excludeFiles

If you have an array of files to check, Get-ChildItem accepts an array of paths:

Get-ChildItem $filesToCheck -exclude $excludeFiles
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
zdan
  • 28,667
  • 7
  • 60
  • 71
  • Thanks - I can confirm that `Get-ChildItem` or simply `dir` (I am more used to Windows terminology) correctly excludes by the same pattern as LIKE, and it works for an array of strings, which can be very convenient for many uses. Unfortunately, it does not work for my needs, because in some cases I need to work with strings directly, not file objects. Still, your answer deserves a +1. – Victor Zakharov Oct 23 '12 at 01:09
1

$excludeFiles.Where{$stringToTest -like $_}

Powershell array has a Where method that can take an expression input, hence the {} instead of (). Feed in a string to test and it will iterate over the array using the standard pipe so $_ represents the element of the array.

Outputs a list of matching elements.

PS H:\> $excludeFiles = @("aaa.bbb", "ccccc.*", "ddddd???.exe")
PS H:\> $stringToTest = "ddddd000.exe"
PS H:\> $excludeFiles.Where{$stringToTest -like $_}
ddddd???.exe