Mats codemix

Mats codemix

A little c#, a litte .NET and throw in some c++ and you get a nice Spaghetti

Mats codemix RSS Feed
 
 
 
 

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}
}

3 Responses to “Powershell-script to find files not having specified text”

  1. 1
    Vincent THAVONEKHAM:

    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

  2. 2
    Vincent THAVONEKHAM:

    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

  3. 3
    matsgf:

    Thanbks for your visit, i will correct the script code to “select-string” instead.

Leave a Reply