【发布时间】:2016-11-25 02:21:26
【问题描述】:
我已经使用 WQL 搜索创建了一个替代 Windows 搜索实用程序的搜索实用程序,它使用 VBScript 进行,但事实证明,它非常慢。我想加快速度,我想我可以做到,但我需要在我的 WQL 搜索之后和 For Each 语句之前放置我的搜索过滤器。这甚至可能吗?
我已经通过 WQL 搜索中的过滤进行了测试,但是如果我在 WQL 搜索之后进行过滤,速度会快 40%。我还测试了有无 iFlags,但它们往往会减慢搜索速度,尽管 MS 似乎不这么认为。
由于用户可以按文件名、创建日期、最后修改日期和/或文件大小进行搜索,如果过滤器位于 For Each 语句之后,则脚本必须在每次枚举文件时创建搜索过滤器。我想创建一次过滤器,希望节省一些搜索时间。
当您查看我发布的代码的 sn-p 时,这可能会更有意义。请注意,子 subCreateSearchString 将调用其他搜索选项和函数(即:从 UTC 转换为本地时间、格式化文件大小等)
Dim strSearchName, strComputer, objSWbemServices, objFile, colFiles
Dim strFileName, strReturnedFileName, strQueryDriveAndPath
strSearchName = "test" 'Text being searched for - change as needed
strQueryDriveAndPath = "PATH = '\\Drop_RW\\' AND DRIVE = 'D:'" 'Path and drive in which to search - change as needed
strComputer = "."
Set objSWbemServices = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objSWbemServices.ExecQuery("Select * from CIM_DataFile WHERE " & "" & strQueryDriveAndPath & "")
'* I'd like to place the call to "subCreateSearchString" here
On Error Resume Next
For Each objFile in colFiles
strReturnedFileName = objFile.Name
subCreateSearchString ' Search filter - it works when placed here
If strSearchForString Then
MsgBox "File matches:" & vbCrLf & strReturnedFileName
Else
MsgBox "File DOES NOT match" & vbCrLf & strReturnedFileName
End If
Next
Sub subCreateSearchString
'* Set Filename Variable for search:
strFileName = InStr(LCase(strReturnedFileName), LCase(strSearchName))
strSearchForString = strFileName
End Sub
【问题讨论】: