2015-10-01 68 views
2

我想通過一個文件列表並檢查每個文件名是否與列表中的任何字符串匹配。這是我迄今爲止的,但它沒有找到任何匹配。我究竟做錯了什麼?PowerShell - 如何檢查字符串以查看它是否包含另一個帶有通配符的字符串?

$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." 
    } 
} 

回答

0

與要使用-like操盤的-match因爲後者需要一個正則表達式通配符。例如:

$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) 
     } 
    } 
} 
相關問題