Leaked source code of windows server 2003
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.

306 lines
8.3 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. locale = ""
  25. forceci = TRUE
  26. inputfile = ""
  27. quiet = FALSE
  28. maxhits = 0
  29. firsthits = FALSE
  30. repeat = 1
  31. ' parse command line arguments
  32. for i = 0 to WScript.Arguments.Count - 1
  33. arg = WScript.Arguments( i )
  34. first = left( arg, 1 )
  35. c = mid( arg, 2, 1 )
  36. if "/" = first or "-" = first then
  37. if ":" = mid( arg, 3, 1 ) then
  38. v = mid( arg, 4 )
  39. select case c
  40. case "e" locale = v
  41. case "f" forceci = ( v = "+" )
  42. case "i" inputfile = v
  43. case "r" repeat = v
  44. case "x" maxhits = v
  45. case "y" maxhits = v
  46. firsthits = TRUE
  47. case else Usage
  48. end select
  49. else
  50. select case c
  51. case "q" quiet = TRUE
  52. case else Usage
  53. end select
  54. end if
  55. else
  56. if "" = query then query = arg else Usage
  57. end if
  58. next
  59. for i = 1 to repeat
  60. if "" = inputfile then
  61. if "" = query then
  62. Usage
  63. end if
  64. DoQuery query, locale, forceci, quiet, maxhits, firsthits
  65. else
  66. if "" <> query then call Usage
  67. ' Open the input file and treat each line as a query.
  68. ' Report errors, but don't stop reading queries.
  69. set fs = WScript.CreateObject( "Scripting.FileSystemObject" )
  70. set f = fs.OpenTextFile( inputfile, 1 )
  71. do until f.AtEndOfStream
  72. line = f.ReadLine
  73. on error resume next
  74. DoQuery line, locale, forceci, quiet, maxhits, firsthits
  75. if 0 <> Err.Number then
  76. out Err.Description
  77. out "The query '" & line & "' failed, error 0x" & Hex( Err.Number )
  78. Err.Clear
  79. end if
  80. out ""
  81. loop
  82. end if
  83. next
  84. end sub
  85. '-------------------------------------------
  86. sub Out( str )
  87. WScript.echo str
  88. end sub
  89. sub Out2( num, str )
  90. out right( space( 9 ) & num, 9 ) & " " & str
  91. end sub
  92. '-------------------------------------------
  93. sub Usage
  94. out "usage: cscript sql.vbs [arguments]"
  95. out " query An Indexing Service query."
  96. out " /e:locale ISO locale identifier, e.g. EN-US; default is system locale."
  97. out " /f:(+|-) + or -, for force use of index. Default is +."
  98. out " /i:inputfile Text input file with queries, one per line."
  99. out " /q Quiet. Don't display info other than query results."
  100. out " /r:# Number of times to repeat the command."
  101. out " /x:maxhits Maximum number of hits to retrieve, default is no limit."
  102. out " /y:firsthits Only look at the first N results."
  103. out ""
  104. out " examples: cscript qu.vbs mango"
  105. out ""
  106. out " locales: af ar-ae ar-bh ar-dz ar-eg ar-iq ar-jo ar-kw ar-lb"
  107. out " ar-ly ar-ma ar-om ar-qa ar-sa ar-sy ar-tn ar-ye be"
  108. out " bg ca cs da de de-at de-ch de-li de-lu e en en"
  109. out " en-au en-bz en-ca en-gb en-ie en-jm en-nz en-tt"
  110. out " en-us en-za es es es-ar es-bo es-c es-co es-cr"
  111. out " es-do es-ec es-gt es-hn es-mx es-ni es-pa es-pe"
  112. out " es-pr es-py es-sv es-uy es-ve et eu fa fi fo fr"
  113. out " fr-be fr-ca fr-ch fr-lu gd gd-ie he hi hr hu in"
  114. out " is it it-ch ja ji ko ko lt lv mk ms mt n neutr"
  115. out " nl-be no no p pt pt-br rm ro ro-mo ru ru-mo s sb"
  116. out " sk sq sr sr sv sv-fi sx sz th tn tr ts uk ur ve"
  117. out " vi xh zh-cn zh-hk zh-sg zh-tw zu"
  118. WScript.Quit( 2 )
  119. end sub
  120. '-------------------------------------------
  121. function FormatValue( v, t )
  122. if 7 = t or 137 = t then
  123. w = 20
  124. 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
  125. w = 7
  126. elseif 20 = t or 21 = t then
  127. w = 12
  128. else
  129. w = 0
  130. end if
  131. if 0 = w then
  132. r = v
  133. else
  134. r = right( space( w ) & v, w )
  135. end if
  136. FormatValue = r
  137. end function
  138. Const STAT_BUSY = 0
  139. Const STAT_ERROR = &H1
  140. Const STAT_DONE = &H2
  141. Const STAT_REFRESH = &H3
  142. Const STAT_PARTIAL_SCOPE = &H8
  143. Const STAT_NOISE_WORDS = &H10
  144. Const STAT_CONTENT_OUT_OF_DATE = &H20
  145. Const STAT_REFRESH_INCOMPLETE = &H40
  146. Const STAT_CONTENT_QUERY_INCOMPLETE = &H80
  147. Const STAT_TIME_LIMIT_EXCEEDED = &H100
  148. Function GetCiOutOfDate(value)
  149. GetCiOutOfDate = value And STAT_CONTENT_OUT_OF_DATE
  150. end Function
  151. Function GetCiQueryIncomplete(value)
  152. GetCiQueryIncomplete = value And STAT_CONTENT_QUERY_INCOMPLETE
  153. end Function
  154. Function GetCiQueryTimedOut(value)
  155. GetCiQueryTimedOut = value And STAT_TIME_LIMIT_EXCEEDED
  156. end Function
  157. '-------------------------------------------
  158. sub DoQuery( query, locale, forceci, quiet, maxhits, firsthits )
  159. if "" <> query then
  160. set Connection = WScript.CreateObject( "ADODB.Connection" )
  161. Connection.ConnectionString = "provider=msidxs;"
  162. Connection.Open
  163. set AdoCommand = WScript.CreateObject( "ADODB.Command" )
  164. AdoCommand.CommandTimeout = 10
  165. AdoCommand.ActiveConnection = Connection
  166. ' AdoCommand.CommandText = "SELECT size,path from SYSTEM..SCOPE('""\""') WHERE CONTAINS('microsoft') AND DOCAUTHOR <> 'David' ORDER BY Path DESC"
  167. AdoCommand.CommandText = query
  168. AdoCommand.Properties("Always Use Content Index") = forceci
  169. if 0 <> maxhits then
  170. if firsthits then
  171. AdoCommand.Properties("First Rows") = maxhits
  172. else
  173. AdoCommand.Properties("Maximum Rows") = maxhits
  174. end if
  175. end if
  176. if "" <> locale then
  177. AdoCommand.Properties("SQL Content Query Locale String") = locale
  178. end if
  179. set rs = WScript.CreateObject( "ADODB.RecordSet" )
  180. rs.CursorType = adOpenKeyset
  181. rs.open AdoCommand
  182. ' Display the results, 20 rows at a time for performance
  183. const cRowsToGet = 20
  184. rs.CacheSize = cRowsToGet
  185. cHits = 0
  186. do until rs.EOF
  187. rows = rs.GetRows( cRowsToGet )
  188. for r = 0 to UBound( rows, 2 )
  189. row = ""
  190. for col = 0 to UBound( rows, 1 )
  191. if 0 <> col then row = row & " "
  192. row = row & FormatValue( rows( col, r ), rs( col ).type )
  193. next
  194. out row
  195. cHits = cHits + 1
  196. next
  197. loop
  198. ' Display query status information
  199. if not quiet then
  200. out CHR(10) & cHits & " files matched the query '" & query & "'"
  201. if GetCiOutofDate(RS.Properties("Rowset Query Status")) then
  202. out "The index is out of date"
  203. end if
  204. if GetCiQueryIncomplete(RS.Properties("Rowset Query Status")) then
  205. out "The query results are incomplete; may require enumeration"
  206. end if
  207. if GetCiQueryTimedOut(RS.Properties("Rowset Query Status")) then
  208. out "The query timed out"
  209. end if
  210. end if
  211. end if
  212. end sub