String PowerShell-如何检查字符串以查看它是否包含另一个带有通配符的字符串?
我想浏览一个文件列表,检查每个文件名是否与列表中的任何字符串匹配。这是我到目前为止所拥有的,但它没有找到任何匹配项。我做错了什么String PowerShell-如何检查字符串以查看它是否包含另一个带有通配符的字符串?,string,powershell,for-loop,pattern-matching,wildcard,String,Powershell,For Loop,Pattern Matching,Wildcard,我想浏览一个文件列表,检查每个文件名是否与列表中的任何字符串匹配。这是我到目前为止所拥有的,但它没有找到任何匹配项。我做错了什么 $files = $("MyApp.Tests.dll","MyApp.Tests.pdb","MyApp.dll") $excludeTypes = $("*.Tests.dll","*.Tests.pdb") foreach ($file in $files) { $containsString = foreach ($type in $Exclude
$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."
}
}
对于通配符,您希望使用
-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)
}
}
}