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.

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