Jump to content

How do you Query Windows Search using ADO?

+ 1
  Alanle [MS}'s Photo
Posted Sep 29 2010 02:49 AM

I was asked an interesting question on my blog. "Is it possible to use AQS to find files that have been modified in the last 10 minutes?"

The answer is Yes. You can go down to minute granularity. I used this syntax on my temp folder

date modified ‎13/11/20099 16:40..13/11/2009 16:50


and it filters out only the files modified between 16:40 and 16:50.

Now the trick to a full answer will be to get the current time and then take 10 minutes off to structure this query. In the Explorer interface, I can't think how you would do it. The only way I can see is via script using ADO.

You can use the WHERE clause within a Select statement to extract the required range of files, for example the above AQS query as a SQL search is

SELECT System.ItemName, System.ItemDate FROM SystemIndex WHERE SCOPE='file:c:/temp' AND (System.ItemDate > '2009-11-13 16:40:00' AND System.ItemDate < '2009-11-13 16:51:00')


The full code I used to try the query out on my system I modified from one of the examples on MSDN. I've added it to the bottom of this post, the other samples can be found on the MSDN page Windows Search Sample Code

I hardcoded the dates and locations to make it easier to read. But as this is VB script (sorry no PowerShell version yet), you could pass the SCOPE and time as arguments.

I used the SCOPE

SCOPE='file:c:/temp'


in the query to look at my Temp folder and all subfolders, I could have used

DIRECTORY='file:c:/temp'


to just scan the TEMP folder when I knew the files I was looking for were and left out the scan of the subfolders.

I wanted to give a flavour of the search functionality in this post, for more information on using SQL can be found on MSDN in the article Querying the Index with Windows Search SQL Syntax
.

Also useful is the list of System Properties you can use. These can be found at Windows Properties: System


Code Sample
rem THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
rem ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
rem THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
rem PARTICULAR PURPOSE.
rem 
rem Copyright (c) Microsoft Corporation. All rights reserved

rem Windows SDK sample that demonstrates how to query Windows Search from script
rem using ADO. 

On Error Resume Next


Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"

objRecordSet.Open "SELECT System.ItemName, System.ItemDate, System.DateModified FROM SystemIndex WHERE SCOPE='file:c:/temp' AND (System.ItemDate > '2009-11-13 16:40:00' AND System.ItemDate < '2009-11-13 16:51:00')", objConnection

objRecordSet.MoveFirst
Do Until objRecordset.EOF
    rem Access the column values that were specified in the SELECT statement here
    Wscript.Echo objRecordset.Fields.Item("System.ItemName")
    Wscript.Echo objRecordSet.Fields.Item("System.DateModified")
    objRecordset.MoveNext
Loop

objRecordset.Close
Set objRecordset = Nothing
objConnection.Close
Set objConnection = Nothing


Tags:
0 Subscribe


0 Replies