00001: ' search.vbs
00002: '
00003: ' This proc does "advanced" file search via the Windows Search database.
00004: ' Adapted from http://technet.microsoft.com/en-us/library/ff404224.aspx
00005: ' Searchable properties list: http://technet.microsoft.com/en-us/library/ff404235.aspx
00006: ' The tool does not search for text within files. This is done nicely by Google Desktop Search.
00007: '
00008: ' Usage: cscript/nologo search.vbs filename | filename.* | *.extension [-q] [> outputfile]
00009: '
00010: ' Note: This tool requires local disks to search to be indexed with the Windows Search Service.
00011: '
00012: ' Input       : file specification
00013: ' Output      : fully qualified list
00014: ' Side effects: none
00015: '
00016: ' v1.0-0 27-oct-2010 DTL Didier.Morandi at gmail.com http://www.scripting-errors.com/
00017: ' v2.0-0  7-nov-2010 DTL add wildcards processing
00018: 
00019: Option Explicit
00020: 
00021: Dim objConnection, objRecordSet, objCommand
00022: Dim intNbFiles, intSize
00023: Dim iStart, iSTop
00024: Dim strTarget, strKey
00025: Dim blnQuiet
00026: 
00027: blnQuiet = False
00028: intNbFiles = 0
00029: intSize = ""
00030: strKey = ""
00031: 
00032: if wscript.arguments.count = 0 then
00033:   wscript.echo _
00034:          "Syntax: search filename | filename.* | *.extension [-q] [> outputfile]"
00035:   wscript.quit
00036: 
00037: elseif wscript.arguments.count = 1 then
00038:   if wscript.arguments(0) = "/?" then 
00039:     wscript.echo _
00040:            "Syntax: search filename | filename.* | *.extension [-q] [> outputfile]"
00041:     wscript.quit
00042:   elseif wscript.arguments(0) = "?" then 
00043:     wscript.echo _
00044:            "Syntax: search filename | filename.* | *.extension [-q] [> outputfile]"
00045:     wscript.quit
00046:   end if
00047: 
00048: elseif wscript.arguments.count = 2 then
00049:   if Lcase(wscript.arguments(1)) <> "-q" then
00050:     wscript.echo _
00051:         "Syntax: search filename | filename.* | *.extension [-q] [> outputfile]"
00052:     wscript.quit
00053:   else
00054:     blnQuiet = True
00055:   end if
00056: end if
00057: 
00058: strTarget = wscript.arguments(0)
00059: 
00060: if right(strTarget,1) = "*" then
00061:   strKey = "System.FileName"
00062:   strTarget = left(strTarget,len(strTarget)-2)
00063: 
00064: elseif left(strTarget,1) = "*" then 
00065:   strKey = "System.FileExtension"
00066:   strTarget = right(strTarget,len(strTarget)-1)
00067: 
00068: else
00069:   strKey = "System.ParsingName"
00070: end if
00071: 
00072: iStart = Timer
00073: 
00074: Set objConnection = CreateObject("ADODB.Connection")
00075: Set objRecordSet  = CreateObject("ADODB.Recordset")
00076: Set objCommand    = CreateObject("ADODB.Command")
00077: 
00078: objConnection.Open _
00079:   "Provider=Search.CollatorDSO;Extended properties='Application=Windows';"
00080: objCommand.ActiveConnection = objConnection
00081: 
00082: objRecordSet.Open "SELECT System.ItemPathDisplay, System.Size FROM SYSTEMINDEX" _
00083:   & " where " & strKey & " = '" & strTarget &"' order by System.Filename", objConnection
00084: objRecordSet.MoveFirst
00085: 
00086: Do Until objRecordset.EOF
00087:   If objRecordSet.EOF Then
00088:     wscript.echo "No such file found"
00089:     wscript.quit 0
00090:   else
00091:     intSize = objRecordset.Fields.Item("System.Size")
00092:     if not blnQuiet then
00093:       if intSize <> "" then
00094:         Wscript.Echo _
00095:                objRecordset.Fields.Item("System.ItemPathDisplay")
00096:       else
00097:         Wscript.Echo _
00098:                objRecordset.Fields.Item("System.ItemPathDisplay") & " [folder]"
00099:       end if
00100:     end if
00101:   intNbFiles = intNbFiles + 1
00102:   objRecordset.MoveNext
00103:   end if
00104: Loop
00105: 
00106: iStop = Timer
00107: 
00108: wscript.echo vbcrlf & intNbFiles & " files found in " & (iStop - iStart) & " seconds among " &_
00109:                                    GetNrOfIndexedFiles() & " entries in index."
00110: wscript.quit 0
00111: 
00112: '-----------------------------------------------------------------------------------
00113: Function GetNrOfIndexedFiles()
00114: 
00115: Dim intNrOfFiles, strComputer, objWMIService, colItems, objItem
00116: 
00117: strComputer = "." 
00118: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
00119: Set colItems = objWMIService.ExecQuery( _
00120:     "SELECT * FROM Win32_PerfRawData_WSearchIdxPi_SearchIndexer",,48) 
00121: 
00122: For Each objItem in colItems 
00123:     intNrOfFiles = objItem.DocumentsFiltered
00124: Next
00125: 
00126: GetNrOfIndexedFiles = intNrOfFiles
00127: 
00128: end function
-----
Listing provided by Line_numbers.vbs on 08/11/2010 13:16:51
(c) 2010 Didier.Morandi (at) gmail dot com
http://www.scripting-errors.com
