PowerShell - 如何检查一个字符串以查看它是否包含另一个带通配符的字符串?
PowerShell - How to check a string to see if it contains another string with wildcard?
我想浏览文件列表并检查每个文件名是否与列表中的任何字符串匹配。到目前为止,这是我所拥有的,但没有找到任何匹配项。我做错了什么?
$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)
}
}
}
我想浏览文件列表并检查每个文件名是否与列表中的任何字符串匹配。到目前为止,这是我所拥有的,但没有找到任何匹配项。我做错了什么?
$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)
}
}
}