3

I want to go through a list of files and check if each filename match any of the string in a list. This is what I have so far, but it is not finding any match. What am I doing wrong?

$files = $("MyApp.Tests.dll","MyApp.Tests.pdb","MyApp.dll")
$excludeTypes = $("*.Tests.dll","*.Tests.pdb")

foreach ($file in $files) 
{
    $containsString = foreach ($type in $ExcludeTypes) { $file | %($_ -match '$type') }

    if($containsString -contains $true)
    {
        Write-Host "$file contains string."
    }
    else
    {
        Write-Host "$file does NOT contains string."
    }
}

1 Answer 1

2

With wildcards you want to use -like operator instead of -match because the latter requires a regular expression. Example:

$files = @("MyApp.Tests.dll","MyApp.Tests.pdb","MyApp.dll")
$excludeTypes = @("*.Tests.dll","*.Tests.pdb")

foreach ($file in $files) {
    foreach ($type in $excludeTypes) {
        if ($file -like $type) { 
            Write-Host ("Match found: {0} matches {1}" -f $file, $type)
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.