简体   繁体   English

如何使用 PowerShell 中的选择字符串查找字符串不匹配的文件?

[英]How to find a file with not match string using select-string in PowerShell?

I have a lot of file.我有很多文件。 I want to find the file which is not contain of a string.我想找到不包含字符串的文件。 I try this, but it return duplicate file name.我试试这个,但它返回重复的文件名。

$File = Get-ChildItem -Path "D:\*.txt"
$Result = Select-String -Pattern "Identifier -NotMatch $File | Select-Object -ExpandProperty FileName
Write-Host "Result: $Result"

It return like this它像这样返回

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

Anyone can help please.任何人都可以帮忙。 Thank you谢谢

-NotMatch will make Select-String return the lines that don't match, it doesn't work at the file level. -NotMatch将使Select-String返回不匹配的,它在文件级别不起作用。

To do what you want, run ALL the files through Select-String and then filter out those that have any matches:做你想做的事,通过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

We grab the first possible match from the file in question - if none is found, the array expression will be empty and Count -eq 0我们从有问题的文件中获取第一个可能的匹配项 - 如果没有找到,则数组表达式将为空并且Count -eq 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM