Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VBScript

Windows Indexing Service Basics

4.50/5 (5 votes)
24 Jun 2013CPOL5 min read 37.2K   438  
This article describes how to manage and use the Windows Indexing Service.

Introduction

When I was creating a virus scanner for an antivirus (college project), I came across a situation where searching for files on the local drives became difficult and time consuming. So I started to search for a better technique for file searching, over the Internet. Then, I came to know about the Windows Indexing Service. Indexing Service can also be used for providing search capabilities on websites.

What is Windows Indexing Service 

Windows Indexing Service is a wonderful utility in Windows NT, Windows 2000, and other Windows NT family of Operating Systems. As described in MSDN, “Indexing Service is a base service for Microsoft® Windows® 2000 or later that extracts content from files and constructs an indexed catalog to facilitate efficient and rapid searching.” With the help of Indexing Service, we can search files for contents, and properties on local host and on remote, networked hosts. Indexing Service uses filters for extracting contents from the files. Every file format has its different filter. Fortunately, Microsoft supplies filters for Microsoft Office files, Hypertext Markup Language (HTML) files, Multipurpose Internet Mail Extension (MIME) messages, and plain-text files. If we want to extraction information from any other format, then we need a filter for that; for example, for PDF files, we need Adobe PDF Ifilter (if you really need it, you can download the Ifilter from here).

What is a Catalog

Indexing Service stores all the indexing information about files in catalogs. By default, only one catalog, called System catalog, is created automatically when we install Indexing Service. There is another catalog called Web catalog, if Internet Information Services (IIS) is installed. The System catalog contains an index for all documents, on all permanently attached disk drives, except certain system and temporary files. Web catalog contains the index of Internet Information Services (IIS).

What is a Scope

After creating a catalog, we add directories to the catalog - this is called scope. Indexing Service will create an index of only those directories and drives which we add to the catalog.

How to Manage Indexing Service

With the release of Indexing Service 3.0, we can perform file system related tasks with several programming languages and several scripting languages. The .NET developer should note that .NET natively doesn’t support access to Indexing Service. So, they need to extend their .NET application to interact with either WMI or COM to manage/access Indexing Service. In this article, we will use VBS for managing and querying Indexing Service. For this, we will use these types of APIs:

  1. Admin Helper
  2. Query Helper

These APIs are available in three classes: AdminIndexServer, CatAdm, and ScopeAdm, declared in the CIODMLib library, so we will use the CreateObject function to access these classes.

VBScript
Set objISAdm = CreateObject("Microsoft.ISAdm") 

Starting/Stopping Indexing Service

To start Indexing Service, we use the Start method, and to stop, we use the Stop method of AdminIndexServer. We can also pause the service by using the Pause method.

VBScript
Set objISAdm = CreateObject("Microsoft.ISAdm")
objISAdm.start()

To check if the service is running or paused, use the IsRunning and IsPaused methods:

VBScript
Set objISAdm = CreateObject("Microsoft.ISAdm")
If objISAdm.IsRunning= False then
    objISAdm.start()
end if

Adding a Catalog

If you want to create a new catalog, use the AddCatalog method:

VBScript
Function AddCatalog(bstrCatName As String, bstrCatLocation As String) As Object

We have to pass two string arguments: the name of the catalog and the path where the catalog will be saved. Please note that the catalog will remain off-line until Indexing Service is restarted. So, the best method is to first stop Indexing Service, add the catalog, and start Indexing Service again. As shown below:

VBScript
Set objISAdm = CreateObject("Microsoft.ISAdm")
objISAdm.stop()
Set objCatalog = objISAdm.AddCatalog("MyCatalog","c:\Mycatalog")
objISAdm.start()

Adding an Indexing Service Scope

As I mentioned above, Indexing Service will create an index of only those directories and drives which we add to the catalog. So here, we will add a scope to our “My_Catalog” catalog using the AddScope method of the Catalog object. For getting the Catalog object, we use the GetCatalogByName method of the AdminIndexServer object as show below:

VB
Set objISAdm = CreateObject("Microsoft.ISAdm")
Set objCatalog = objISAdm. GetCatalogByName("MyCatatlog")
Set objScope= objCatalog.AddScope("C:\myfiles",False)
objScope.Alias = "MyCatalogScope"

The AddScope method takes four parameters:

  • strScopePath - Required. The scope path to create.
  • blnExclude - Required. If TRUE, excludes this scope from indexing. If FALSE, enables indexing for this scope.
  • vtLogon - Optional. The string that represents the logon name.
  • vtPassword - Optional. The string that represents the logon password.

vtLogon and vtPassword are both required if the scope path is a UNC path. The Alias property is the case-insensitive name of the alias or friendly name for this scope. This property is read/write.

Querying an Indexing Server Catalog

Using queries, we can find files with words and phrases in their contents, or their properties such as the document's name or author. The simplest kind of query is a single word. For example, if you search for “programs”, Indexing Service returns all documents that contain the word “programs” in their contents. There are five kinds of queries:

Free-text and Phrase queries

With free-text and Phrase queries, we can search files for a group of words or a complete sentence. Indexing Service finds pages that best match the words and phrases in the Free-text and Phrase query.

Pattern-matching queries

In Pattern-matching queries, we can use wildcards and Regular Expressions. For example, “@filename=*.doc” will search all files with a “doc” extension.

Vector-space queries

In Vector-space queries, we assign weighting factors to control the relative importance of terms.

Example Querying an Indexing Server Catalog

VBScript
set q = WScript.CreateObject( "ixsso.Query" )
q.Query = "@filename= *.vbs"
q.Catalog = "query://./SYSTEM"

q.AllowEnumeration =False
q.Dialect = 2
q.Columns = "FileName"
set rs = q.CreateRecordSet( "sequential" )

Do Until rs.EOF
    msgbox(rs("FileName"))
    rs.MoveNext
Loop

In this example, we search all files that have a “vbs” extension. We used the “SYSTEM” catalog here.

Summary

Indexing service is a powerful feature in the Windows Operating System. Moreover, it is free, easy to use, and very flexible. It can be used for both web and desktop applications. I hope this article helps you to understand Indexing Service and to better use it.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)