Is there a way to ignore Windows search indexing for certain patterns?
Asked Answered
M

2

7

Is there a way to ignore Windows search indexing for certain patterns?

For example, I would like to skip the following folders:

C:\Git**.git\ C:\Git**\packages\ C:\misc\git**.git\ C:\misc\git**\packages\

These cause thousands of extra noise matches in search results.

I do want to index the various folders in C:\misc\git except for what's in the .git or .packages subfolders. I've only found a static way to do this, and nothing based on the pattern. It seems like this would be a common use case and is a little bit like the way git uses .gitignore to keep certain content out of version control. In this case, it would be keeping it out of the search index.

I do not see any facility for patterns like this in "Indexed Locations". I assume it could be done by running some kind of script to exclude the current matches each time content changed (e.g. C:\misc\git\somethingnew was added) but that would be the opposite of elegant.

Any help would be appreciated.

Penny

Meditate answered 19/4, 2017 at 19:38 Comment(1)
This is pretty useful as well superuser.com/questions/235799/…Regimentals
R
4

According to this answer by @PryrtCJ Windows registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Search\CrawlScopeManager\Windows\SystemIndex\WorkingSetRules\ contains rules, which allow to use wildcards.

regedit with *.git\ exclude rule

To add such rules I suggest to add one .git folder in indexing options to excluded paths and then find proper key (URL would be sth. like file:///C:\[partition-or-disk-id]\path\to\.git\). Then edit key permissions (select key containing the .git URL and right-click it) - you'll need to change the key owner from SYSTEM to your account and allow Administrators group to modify key.

advanced permissions settings

If you have permissions to modify it, right-click URL, select modify and change it's content to file:///*\.git\. Later restore previous permissions (just to avoid problems).

This way you can create any * filters so eg. file:///C:\misc\*\packages\*

Rosenberger answered 18/5, 2018 at 8:15 Comment(1)
Somehow the .git folder that I excluded indexing doesn't appear in the registryNumb
L
3

The Windows Search Index can be managed programmatically.

You can exclude folders from the Search Index using the following patterns :

  • file:///C:\*\packages\ matches a folder named "packages" and its content
  • file:///D:\*\obj\ matches a folder named "obj" and its content

I tested these patterns successfully using C# under Windows 11 21h2.

Example to add a new rule (Powershell)

# [wsearch-add-rule.ps1]
# Source: https://mcmap.net/q/1622811/-how-to-add-a-location-to-windows-7-8-search-index-using-batch-or-vbscript/13454571#13454571
Add-Type -path ".\Microsoft.Search.Interop.dll"
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass 
$cat = $sm.GetCatalog("SystemIndex")
$csm = $cat.GetCrawlScopeManager()
$csm.AddUserScopeRule("file:///D:\*\obj\", $true, $false, $null)
$csm.SaveAll()

Example to list existing rules (Powershell)

# [wsearch-list-rules.ps1]
# Source: https://powertoe.wordpress.com/2010/05/17/powershell-tackles-windows-desktop-search/
Add-Type -path "Microsoft.Search.Interop.dll"
$sm = New-Object Microsoft.Search.Interop.CSearchManagerClass 
$cat = $sm.GetCatalog("SystemIndex")
$csm = $cat.GetCrawlScopeManager()
$scopes = @()
$begin = $true
[Microsoft.Search.Interop.CSearchScopeRule]$scope = $null
$enum = $csm.EnumerateScopeRules() 
while ($scope -ne $null -or $begin) {
     $enum.Next(1,[ref]$scope,[ref]$null)
     $begin = $false
     $scopes += $scope
}
$scopes|ogv

The required Microsoft.Search.Interop.dll can be found in the old Windows Search 3x SDK on web archive (the url must be copied and pasted in a new tab)

Resources :

Lignocellulose answered 18/1, 2022 at 20:17 Comment(2)
Is the only way to manage it by installing an external dll?Martine
Microsoft.Search.Interop.dll is required when the index is handled via .NET (managed project or Powershell). Note that the dll can also be generated on the fly. More info hereLignocellulose

© 2022 - 2024 — McMap. All rights reserved.