Windows Search Services SystemIndex does not search for contents of sub-folders

Updated on: May 27, 2021

Know the solution below when files inside all folders inside the parent folder and its sub folder are not getting retrieved in the search result when searching with Windows Search Services SystemIndex. 

We are considering that you already have set the folder to index in Indexing Options. If you want to know how to set the Indexing Options, then refer http://www.techtutorhub.com/article/ASP-NET-Core-Windows-Search-Services-Indexing-Options-in-Windows-10/53 

Solution:

string strQuery = "SELECT System.ItemName, System.FileDescription, System.DateModified FROM SystemIndex " +
                          @"WHERE DIRECTORY
 = 'file:F:/' AND System.Itemname LIKE '%e%' "; 

If you search from your .NET or PHP or python code using above statement, then it will only give result of all the files from F:/ drive, it will not search on folders and its contents. If you want to do search on all files inside F:/ drive and all folder and its contents in F:/ then you need to change the select query as shown below:

string strQuery = "SELECT System.ItemName, System.FileDescription, System.DateModified FROM SystemIndex " +
                          @"WHERE SCOPE = 'file:F:/' AND System.Itemname LIKE '%e%' ";

If you see from above code, we have just changed the where clause, in this where clause we have used SCOPE instead of Directory. So when you use SCOPE it searches for all the contents inside the path specified including sub-folders and its contents. Using above select query your problem of Search contents on sub folders will get solved.

If you want to Skip any directory on the search result then you can put the AND condition on the above select query as shown below:

string strQuery = "SELECT System.ItemName, System.FileDescription, System.DateModified FROM SystemIndex " +
                          @"WHERE SCOPE = 'file:F:/' AND NOT SCOPE ='file:F:/Documents/MyDocs' AND System.Itemname LIKE '%e%' ";