• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

Search Folder Recursively and Report "No matches"

This is a 'continuation' of this question.

Originally, I had asked that, given a list of keywords, and a pointer to a folder, I would like to display all occurrences of where any of the keywords were found, and indicate if no matches were found for that keyword. I have over 300 keywords and about 250 SQL scripts that make up several hundred thousand lines of code, so I am changing the requirements somewhat (although I am impressed how fast PowerShell does this).

The reason for showing lines that included the keywords was so I could check for scenarios where a substring was found (like I_FIS as a keyword found in I_FIS_REJECT), but the output is overwhelming.

Having executed the code suggested in the above link, I am refining the parameters.
First of all, the keywords are table names, and I am checking code to see if the table is referenced. As such, the keyword would almost always be either a full word (such as SELECT * FROM mytable) or be prefixed with a period (.), such as SELECT * FROM dbo.mytable). There should always be whitespace in the form of a space or CR/LF following the keyword, as shown by these examples:
FROM dbo.mytable
WHERE 1 = 1
FROM mytable t
WHERE 1 = 1

I want the search and report to be case insensitive, such that, if my keyword is "mytable", "MyTable" and "MYTABLE" count as a match, and I want to report where the keyword is NOT FOUND.

My original request just stated that I would search a folder and subfolders for any file, but I only want to search files with a file type of ".SQL"

The output only has to list the keywords from the list that were not found in any of the scripts.
1 Solution
Try the following.  Once it finds a match in any file it records that keyword, then skips to processing the next keyword, and so on.  At the end the recorded matches are compared against the original list of keywords, and any that are in the original list but not in recorded matches list are output.
$startFolder = "c:\temp"
$patterns = Get-Content "search.txt"
$result = @(foreach ($pattern in $patterns)
    $regex = "[\. ]$pattern\b"
    Get-ChildItem $startFolder -filter "*.sql" -Recurse -force | Where { !($_.PsIsContainer) } | ForEach `
        If (Select-String -InputObject $_ -Pattern $regex -Quiet)

Compare-Object $patterns $result -PassThru | ForEach `
{ Write-Output """$_"" was not found" }

Open in new window

dbbishopAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

WEBINAR: GDPR Implemented - Tips & Lessons Learned

Join the WatchGuard team on Thursday, March 29th as we recount some valuable lessons learned in weighing the needs of a business against the new regulatory environment, look ahead at the two months left before implementation, and help you understand the steps you can take today!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now