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."
}
}
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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With