Powershell-script to find files not having specified text
I had the need to find files “not containing” a certain text.
I scanned the web and found all and nothing. A few dedicated search tools and editors (some payware and some free).
Since i use powershell a lot in my work i wanted to use that environment. Finding a file based on a certain text is easy with the select-string or combination of get-childitem and select-string.
Maybe there is a solution that does not include scripting? I would be happy to hear what the options are.
I have the feeling that this can be solved as a filter-script (begin,process,end)?
New revised version that outputs results of a get-childitem filtered through a where-object.
# Find all files which does not contains the text in $Pattern
function Get-FilesHasNot(
[Parameter(Mandatory=$true)]
[string] $Path=”*.txt”
,[Parameter(Mandatory=$true)]
[string] $pattern=”"
)
{
$has=(get-childitem $path | select-string $pattern) | foreach {$_.Path}
get-childitem $path| where {$has.Contains($_.FullName) -eq $false}
}
January 3rd, 2012 at 11:15
Hi, thanks for that.
I wanted to know, what is “ss” ?? in ss $pattern ??
Indeed, I got the following error:
“The term ’ss’ is not recognized as the name of a cmdlet, function, script file, or operable program.”
Regards,
Vincent THAVONEKHAM
January 3rd, 2012 at 11:54
OK, I got is: “ss” stands for “select-string”.
It works fine, thanks.
Here is an example to check if StyleCop has been activated in ALL projects:
function ssHasNot(
[string] $Path=”*.csproj”
,[string] $pattern=”StyleCop.target”
)
{
$has=[string]@(get-childitem $path | select-string $pattern | foreach {$_.Path})
get-childitem $path| where {$has.Contains($_.FullName) -eq $false}
}
ssHasNot
Vincent THAVONEKHAM
January 3rd, 2012 at 12:34
Thanbks for your visit, i will correct the script code to “select-string” instead.