Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

  1. '+---------------------------------------------------------------------
  2. '
  3. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4. ' ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  5. ' THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  6. ' PARTICULAR PURPOSE.
  7. '
  8. ' Copyright (c) Microsoft Corporation, 1999. All Rights Reserved.
  9. '
  10. ' SCRIPT: QSample.vbs
  11. '
  12. ' PURPOSE: Illustrates using VBScript, Query Helper API, and Admin
  13. ' Helper API with Windows Scripting Host to query Indexing
  14. ' Service and to determine the catalog status. The script
  15. ' functions similarly to the C++ Simple(QSample) sample.
  16. '
  17. ' PLATFORM: Windows 2000
  18. '
  19. '----------------------------------------------------------------------
  20. main
  21. '----------------------------------------------------------------------
  22. sub Main
  23. if WScript.Arguments.Count < 1 then call Usage end if
  24. ' set defaults for all arguments
  25. query = ""
  26. catalog = "system"
  27. locale = ""
  28. forceci = TRUE
  29. forcemerge = FALSE
  30. inputfile = ""
  31. shallow = FALSE
  32. dialect = 1
  33. machine = "."
  34. columns = "path"
  35. scope = "\"
  36. quiet = FALSE
  37. sort = ""
  38. stats = FALSE
  39. uptodate = FALSE
  40. maxhits = 0
  41. repeat = 1
  42. ' parse command line arguments
  43. for i = 0 to WScript.Arguments.Count - 1
  44. arg = WScript.Arguments( i )
  45. first = left( arg, 1 )
  46. c = mid( arg, 2, 1 )
  47. if "/" = first or "-" = first then
  48. if ":" = mid( arg, 3, 1 ) then
  49. v = mid( arg, 4 )
  50. select case c
  51. case "c" catalog = v
  52. case "e" locale = v
  53. case "f" forceci = ( v = "+" )
  54. case "i" inputfile = v
  55. case "l" dialect = v
  56. case "m" machine = v
  57. case "o" columns = v
  58. case "p" scope = v
  59. case "r" repeat = v
  60. case "s" sort = v
  61. case "x" maxhits = v
  62. case else Usage
  63. end select
  64. else
  65. select case c
  66. case "g" forcemerge = TRUE
  67. case "j" shallow = TRUE
  68. case "q" quiet = TRUE
  69. case "t" stats = TRUE
  70. case "u" uptodate = TRUE
  71. case else Usage
  72. end select
  73. end if
  74. else
  75. if "" = query then query = arg else Usage
  76. end if
  77. next
  78. ' Turn a relative scope path into an absolute path
  79. if "\" <> scope and "\\" <> left( scope, 2 ) then
  80. set fso = WScript.CreateObject( "Scripting.FileSystemObject" )
  81. scope = fso.GetAbsolutePathName( scope )
  82. end if
  83. for i = 1 to repeat
  84. if "" = inputfile then
  85. if "" = query and not ( stats or uptodate or forcemerge ) then
  86. Usage
  87. end if
  88. DoQuery query, catalog, locale, forceci, forcemerge, shallow, dialect, machine, columns, scope, quiet, sort, stats, uptodate, maxhits
  89. else
  90. if "" <> query then call Usage
  91. ' Open the input file and treat each line as a query.
  92. ' Report errors, but don't stop reading queries.
  93. set fs = WScript.CreateObject( "Scripting.FileSystemObject" )
  94. set f = fs.OpenTextFile( inputfile, 1 )
  95. do until f.AtEndOfStream
  96. line = f.ReadLine
  97. on error resume next
  98. DoQuery line, catalog, locale, forceci, forcemerge, shallow, dialect, machine, columns, scope, quiet, sort, stats, uptodate, maxhits
  99. if 0 <> Err.Number then
  100. out Err.Description
  101. out "The query '" & line & "' failed, error 0x" & Hex( Err.Number )
  102. Err.Clear
  103. end if
  104. out ""
  105. loop
  106. end if
  107. next
  108. end sub
  109. '----------------------------------------------------------------------
  110. sub Out( str )
  111. WScript.echo str
  112. end sub
  113. sub Out2( num, str )
  114. out right( space( 9 ) & num, 9 ) & " " & str
  115. end sub
  116. '----------------------------------------------------------------------
  117. sub Usage
  118. out "usage: cscript qsample.vbs <query> [arguments]"
  119. out " <query> An Indexing Service query."
  120. out " arguments:"
  121. out " /c:catalog Name of the catalog, default is SYSTEM"
  122. out " /e:locale ISO locale identifier, e.g. EN-US; default is system locale."
  123. out " /f:(+|-) + or -, for force use of index. Default is +."
  124. out " /g Force a master merge."
  125. out " /i:inputfile Text input file with queries, one per line."
  126. out " /j Just return files in the scope path, and not subdirectories."
  127. out " /l:dialect 1 or 2, for old or new dialect, default is 1."
  128. out " /m:machine Name of the machine, default is local machine."
  129. out " /o:columns Output column list, default is path."
  130. out " /p:scope The scope path of the query, absolute or relative."
  131. out " /q Quiet. Don't display info other than query results."
  132. out " /r:# Number of times to repeat the command."
  133. out " /s:sort Sort column list, default is none. e.g.: write[d]."
  134. out " Append [a] for ascending (default) or [d] for descending."
  135. out " /t Display catalog statistics."
  136. out " /u Check if the catalog is up to date."
  137. out " /x:maxhits Maximum number of hits to retrieve, default is no limit."
  138. out ""
  139. out " examples: cscript qsample.vbs mango /o:size,path"
  140. out " cscript qsample.vbs ""peach and not apple"" /s:rank[d] /p:."
  141. out " cscript qsample.vbs ""@size > 1000000"" /o:size,path /s:size[a] /m:dogfood"
  142. out " cscript qsample.vbs ""@docauthor joe"" /o:docauthor,path /s:docauthor,path"
  143. out " cscript qsample.vbs apricot /p:c:\\files"
  144. out " cscript qsample.vbs /m:index1 /c:sources pear"
  145. out ""
  146. out " columns: path vpath directory filename write create size attrib"
  147. out " rank hitcount workid fileindex"
  148. out " docauthor doclastauthor dockeywords docsubject doctitle"
  149. out ""
  150. out " locales: af ar-ae ar-bh ar-dz ar-eg ar-iq ar-jo ar-kw ar-lb"
  151. out " ar-ly ar-ma ar-om ar-qa ar-sa ar-sy ar-tn ar-ye be"
  152. out " bg ca cs da de de-at de-ch de-li de-lu e en en"
  153. out " en-au en-bz en-ca en-gb en-ie en-jm en-nz en-tt"
  154. out " en-us en-za es es es-ar es-bo es-c es-co es-cr"
  155. out " es-do es-ec es-gt es-hn es-mx es-ni es-pa es-pe"
  156. out " es-pr es-py es-sv es-uy es-ve et eu fa fi fo fr"
  157. out " fr-be fr-ca fr-ch fr-lu gd gd-ie he hi hr hu in"
  158. out " is it it-ch ja ji ko ko lt lv mk ms mt n neutr"
  159. out " nl-be no no p pt pt-br rm ro ro-mo ru ru-mo s sb"
  160. out " sk sq sr sr sv sv-fi sx sz th tn tr ts uk ur ve"
  161. out " vi xh zh-cn zh-hk zh-sg zh-tw zu"
  162. WScript.Quit( 2 )
  163. end sub
  164. '----------------------------------------------------------------------
  165. function FormatValue( v, t )
  166. if 7 = t or 137 = t then
  167. w = 20
  168. elseif 2 = t or 3 = t or 4 = t or 5 = t or 14 = t or 17 = t or 18 = t or 19 = t then
  169. w = 7
  170. elseif 20 = t or 21 = t then
  171. w = 12
  172. else
  173. w = 0
  174. end if
  175. if 0 = w then
  176. r = v
  177. else
  178. r = right( space( w ) & v, w )
  179. end if
  180. FormatValue = r
  181. end function
  182. '----------------------------------------------------------------------
  183. sub DoQuery( query, catalog, locale, forceci, forcemerge, shallow, dialect, machine, columns, scope, quiet, sort, stats, uptodate, maxhits )
  184. if "" <> query then
  185. ' Create the query object and set the query properties
  186. set q = WScript.CreateObject( "ixsso.Query" )
  187. q.Query = query
  188. q.Catalog = "query://" & machine & "/" & catalog
  189. q.AllowEnumeration = not forceci
  190. q.Dialect = dialect
  191. q.Columns = columns
  192. q.CiScope = scope
  193. if shallow then q.CiFlags = "shallow"
  194. if "" <> sort then q.SortBy = sort
  195. if 0 <> maxhits then q.MaxRecords = maxhits
  196. if "" <> locale then
  197. set u = WScript.CreateObject( "ixsso.Util" )
  198. q.LocaleId = u.ISOToLocaleID( locale )
  199. end if
  200. set rs = q.CreateRecordSet( "sequential" )
  201. ' Display the results, 20 rows at a time for performance
  202. const cRowsToGet = 20
  203. rs.CacheSize = cRowsToGet
  204. cHits = 0
  205. do until rs.EOF
  206. rows = rs.GetRows( cRowsToGet )
  207. for r = 0 to UBound( rows, 2 )
  208. row = ""
  209. for col = 0 to UBound( rows, 1 )
  210. if 0 <> col then row = row & " "
  211. row = row & FormatValue( rows( col, r ), rs( col ).type )
  212. next
  213. out row
  214. cHits = cHits + 1
  215. next
  216. loop
  217. ' Display query status information
  218. if not quiet then
  219. out CHR(10) & cHits & " files matched the query '" & query & "'"
  220. if q.OutOfDate then
  221. out "The index is out of date"
  222. end if
  223. if q.QueryTimedOut then
  224. out "The query timed out"
  225. end if
  226. if q.QueryIncomplete then
  227. out "The query results are incomplete; may require enumeration"
  228. end if
  229. end if
  230. end if
  231. ' Display catalog status information and/or force a merge
  232. if stats or uptodate or forcemerge then
  233. set a = WScript.CreateObject( "microsoft.ISAdm" )
  234. a.MachineName = machine
  235. set c = a.GetCatalogByName( catalog )
  236. if forcemerge then c.ForceMasterMerge
  237. if stats then
  238. out "Machine: " & machine
  239. out "Catalog: " & catalog
  240. out "Location: " & c.CatalogLocation
  241. out2 c.TotalDocumentCount, "Documents in the catalog"
  242. out2 c.FreshTestCount, "Documents modified since the last master merge"
  243. out2 c.FilteredDocumentCount, "Documents filtered since the service started"
  244. out2 c.DocumentsToFilter, "Documents to filter"
  245. out2 c.DelayedFilterCount, "Documents deferred for filtering"
  246. out2 c.UniqueKeyCount, "Unique keys in the master index"
  247. out2 c.WordListCount, "Wordlists"
  248. out2 c.PersistentIndexCount, "Saved indexes"
  249. out2 c.QueryCount, "Queries executed since the service started"
  250. out2 c.IndexSize, "Megabytes used for index files"
  251. out2 c.PendingScanCount, "Scans scheduled"
  252. s = c.StateInfo
  253. if s and &H1 then out "Shadow merge " & c.PctMergeComplete & "% complete"
  254. if s and &H2 then out "Master merge " & c.PctMergeComplete & "% complete"
  255. if s and &H8 then out "Annealing merge " & c.PctMergeComplete & "% complete"
  256. if s and &H20 then out "Recovery in progress..."
  257. if s and &H80 then out "Indexing paused due to low memory"
  258. if s and &H100 then out "Indexing paused due to high system I/O"
  259. if s and &H400 then out "Catalog is read-only"
  260. if s and &H800 then out "Indexing paused due to running on battery power"
  261. if s and &H1000 then out "Indexing paused due to busy interactive user"
  262. if s and &H2000 then out "Indexing service is starting..."
  263. if s and &H4000 then out "Reading the NTFS USN log(s)"
  264. end if
  265. if uptodate then
  266. if c.IsUpToDate then
  267. out "The catalog is up to date."
  268. else
  269. out "The catalog is not up to date."
  270. end if
  271. end if
  272. end if
  273. end sub