[英]How to find a file with not match string using select-string in PowerShell?
我有很多文件。 我想找到不包含字符串的文件。 我试试这个,但它返回重复的文件名。
$File = Get-ChildItem -Path "D:\*.txt"
$Result = Select-String -Pattern "Identifier -NotMatch $File | Select-Object -ExpandProperty FileName
Write-Host "Result: $Result"
它像这样返回
1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 1589.txt 158A.txt 158A.txt 158A.txt 158A.txt 158A.txt 158A.txt AB8A.txt AB8A.txt AB8A.txt AB8A.txt AB8A.txt
任何人都可以帮忙。 谢谢
-NotMatch
将使Select-String
返回不匹配的行,它在文件级别不起作用。
做你想做的事,通过Select-String
运行所有文件,然后过滤掉那些有任何匹配的文件:
$result = Get-ChildItem -Path D:\ -Filter *.txt |Where-Object {
@($_ |Select-String -Pattern "Identifier" |Select-Object -First 1).Count -eq 0
} |Select-Object -ExpandProperty FileName
我们从有问题的文件中获取第一个可能的匹配项 - 如果没有找到,则数组表达式将为空并且Count -eq 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.