The FileLocator Pro core search engine can be used from within many late binding scripting environments, including VBScript. The following example was kindly provided by Alex K. Angelopoulos and show how to use the search engine in both synchronous (i.e. blocking) and asynchronous modes:
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2009
'
' NAME: FLProDemo.vbs
'
' AUTHOR: Alex K. Angelopoulos,
' DATE : 2009-06-05
'
' COMMENT:
'
'==========================================================================
' NOTE: Special constants are used in several locations here. They are all
' declared at the end of the code.
Option Explicit
' First, in order to make the script as easy to run as possible, we want to find
' a known folder to use for searches. For us it will be the Windows directory.
' You can specify the search path for this demo script any way you like;
' just change the value of the LookInDirectory to an explicit path, like this:
' LookInDirectory = "C:\Windows"
' However, we'll use some other functionality in the library to find the "proper"
' directory for an arbitrary machine.
' We can use Shell.Application, but the FlProCore COM library exposes a MachineEnvironment
' class that simplifies things immensely:
Dim MachineEnvironment
Set MachineEnvironment = CreateObject("FLProCore.MachineEnvironment")
' Find Windows. Note that I'm using a constant (with literal value of 36) that
' FLPro knows means "the machine Windows folder, whatever its name". So if you've
' somehow wound up with Windows in E:\Winold.002 or another odd place, this finds it.
' Constants declared at end of script.
Dim LookInDirectory
LookInDirectory = MachineEnvironment.GetSpecialFolderPath(SpecialFolderWindowsFolder)
' We're going to demo looking for log files. To simplify modifying the demo, I set them
' up here, together.
Dim FileDosSpec: FileDosSpec = "*.log"
Dim FileRegexPattern: FileRegexPattern = "\.log$"
Dim FlProEngine
Set FlProEngine = CreateObject("FLProCore.SearchEngine")
' ===== Configuring general search criteria.
' First, tell it to search subdirectories (for demo; true is default anyway)
FlProEngine.SearchCriteria.SearchSubDirectory = True
' select the path under which we'll look.
' Multiple paths can be specified by joining them with semicolons
FlProEngine.SearchCriteria.LookIn = LookInDirectory
' Choose to NOT include folders with conforming names in the search results
FlProEngine.SearchCriteria.FileAttributes.Folder = False
' default for search expression type is DOS (standard DOS wildcards)
' We'll set it here explicitly just to show it:
FlProEngine.SearchCriteria.FileNameExprType = ExpressionDosExp ' = 4
' now provide a legitimate DOS filespec
FlProEngine.SearchCriteria.FileName = FileDosSpec
' start the search, and use False to ensure it is synchronous.
Dim ResultList ' this will be used for our SearchResultItemList instance
Set ResultList = FlProEngine.Start(false)
' The following will not be necessary with versions of the engine
' released after 2009 June or so.
FlProEngine.Cancel
WScript.Echo "Result Count:", ResultList.Count
' We can loop through the results with
' For each result in ResultList... Next
' However, using a counter will show how to access items by index.
Dim i, ResultItem
For i = 0 to ResultList.Count -1
Set ResultItem = ResultList.Item(i)
' We already filtered out folders, but I'm doing it here to demonstrate how
' you could post-filter results.
' it also allows me to exercise the ResultItem with a shorter final
' displayed line...
If Not ResultItem.IsFolder Then
Dim filepath
' the Path property is actually the path to the _parent folder_,
' not the item itself.
' if we're really obsessive-compulsive, we can use
' Scripting.FileSystemObject's
' BuildPath method with Path and FileName, like this:
' Dim fso
' Set fso = CreateObject("Scripting.FileSystemObject")
' filepath = fso.BuildPath(ResultItem.Path, ResultItem.FileName)
'
' that's pointless, however, since Path is always well-formed
' with a terminal \ (from what I can tell).
' instead, just assemble them like so:
filepath = ResultItem.Path & ResultItem.FileName
WScript.Echo i, ResultItem.Path, ResultItem.FileName, filepath, ResultItem.ModifiedDate, ResultItem.Size
End If
' ResultItem.TextLineList
Next
' ===================================
' Regex search example
' We could instead look for files whose names match a pattern
FlProEngine.SearchCriteria.FileNameExprType = ExpressionRegExp ' = 2
' Now the SearchCriteria.FileName needs to be a regular expression pattern.
FlProEngine.SearchCriteria.FileName = FileRegexPattern
'
Set ResultList = FlProEngine.Start(false)
FlProEngine.Cancel
' and for brevity, let's just confirm it worked by echoing the count.
' The TYPE of the filename for search has no effect on the type of the results.
WScript.Echo "Result Count:", ResultList.Count
' and now, with events!
Set FlProEngine = WScript.CreateObject("FLProCore.SearchEngine", "FlPro_")
' we need to set up our search again. Let's do a simple Windows directory
' search for log files using a minimalist DOS wildcard.
FlProEngine.SearchCriteria.LookIn = LookInDirectory
FlProEngine.SearchCriteria.FileName = FileDosSpec
FlProEngine.SearchCriteria.FileAttributes.Folder = False
' Alternatively, we could also just keep our standard
' Set FlProEngine = CreateObject("FLProCore.SearchEngine")
' and then hook up events fired by the engine 1-by-1 using GetRef. To hook
' the engine's OnProgress event to a subroutine named FlPro_OnProgress,
' we would use the following syntax:
' Set FlProEngine.OnProgress = GetRef("FlPro_OnProgress")
' However, since we used WScript.CreateObject WITH an event handler prefix,
' events will now automatically hook up to routines using names of the form
' prefix + eventname (they also need to explicitly declare names for data
' passed from the event.
' With our FlPro_ prefix, the following routines will work as event handlers:
'Sub FlPro_OnFileFound(pIFileFoundList)
'End Sub
'Sub FlPro_OnProgress(nCurrentPhase, strLocation, nContentTotal, nFileTotal)
'End Sub
'Sub FlPro_OnSearchFinish
'End Sub
'Sub FlPro_OnSearchStart
'End Sub
' All we need to do at this point is start a search, BUT this time
' we want to say TRUE - yes, we do want to use alternate threads.
' Also note that although we can handle the files in the FlPro_OnFileFound
' routine, the current result set is available in ResultList at all times.
WScript.Echo "Starting search at", Time
Set ResultList = FlProEngine.Start(True)
' Now we need to wait. From now on, the script's main thread does nothing
' but camp on this loop; any action happens in the event handler subroutines
'beyond the loop.
Do While True
'Note: UNLIKE graphical applications, scripts do need to sleep
'when doing async work.
WScript.Sleep 20
Loop
Sub FlPro_OnFileFound(FoundList)
' This is a resultlist
WScript.Echo Time, FoundList.Count, "files returned from the search engine"
End Sub
Sub FlPro_OnSearchFinish()
' give a brief message, including a demo that ResultList does indeed work.
WScript.Echo "search completed at", Time, "with", ResultList.Count, "files found"
WScript.Quit
End Sub
'==========================================================================
' FLPro constants listing.
' Names are based on combining the name of the eontaining enumeration
' (minus terminal "-Type" if present) with the actual constant name within
' the library. This prevents collisions.
'
' AUTHOR: Alex K. Angelopoulos,
' DATE : 2009-06-05
'==========================================================================
'ExpressionType
const ExpressionBoolean = 0
const ExpressionExact = 1
const ExpressionRegExp = 2
const ExpressionRESERVED = 3
const ExpressionDosExp = 4
'ExportFormatType
const ExportFormatText = 0
const ExportFormatCommaSeparated = 1
const ExportFormatTabSeparated = 2
const ExportFormatXML = 3
const ExportFormatHTML = 4
'AttributeState
const AttributeStateEither = 0
const AttributeStateOff = 1
const AttributeStateOn = 2
'RegularExpressionType
const RegularExpressionBoost = 0
const RegularExpressionClassic = 1
'ExtensionPlugInType
const ExtensionPlugInUnknown = 0
const ExtensionPlugInCompositeFile = 1
const ExtensionPlugInTextConverter = 2
const ExtensionPlugInTextInterpreter = 3
'RelativeDateType
const RelativeDateAbsoluteDate = 0
const RelativeDateToday = 1
const RelativeDateStartOfWeek = 2
const RelativeDateStartOfMonth = 3
const RelativeDateStartOfYear = 4
'DateResolutionType
const DateResolutionDays = 0
const DateResolutionWeeks = 1
const DateResolutionMonths = 2
const DateResolutionYears = 3
'RelativeTimeType
const RelativeTimeAbsoluteTime = 0
const RelativeTimeNow = 1
const RelativeTimeStartOfMinute = 2
const RelativeTimeStartOfHour = 3
const RelativeTimeStartOfDay = 4
'TimeResolutionType
const TimeResolutionSecond = 0
const TimeResolutionMinute = 1
const TimeResolutionHour = 2
'SpecialFolderType
const SpecialFolderProgramsGroup = 2
const SpecialFolderMyDocuments = 5
const SpecialFolderFavourites = 6
const SpecialFolderStartup = 7
const SpecialFolderRecent = 8
const SpecialFolderSendTo = 9
const SpecialFolderStartMenu = 11
const SpecialFolderMyMusic = 13
const SpecialFolderMyVideo = 14
const SpecialFolderDesktopFolder = 16 '(&H10)
const SpecialFolderMyNetwork = 19 '(&H13)
const SpecialFolderFonts = 20 '(&H14)
const SpecialFolderDocumentTemplates = 21 '(&H15)
const SpecialFolderCommonStartMenu = 22 '(&H16)
const SpecialFolderCommonProgramsGroup = 23 '(&H17)
const SpecialFolderCommonStartup = 24 '(&H18)
const SpecialFolderCommonDesktopFolder = 25 '(&H19)
const SpecialFolderApplicationData = 26 '(&H1A)
const SpecialFolderMyPrinters = 27 '(&H1B)
const SpecialFolderLocalApplicationData = 28 '(&H1C)
const SpecialFolderAlternativeStartup = 29 '(&H1D)
const SpecialFolderCommonAlternativeStartup = 30 '(&H1E)
const SpecialFolderCommonFavourites = 31 '(&H1F)
const SpecialFolderInternetCache = 32 '(&H20)
const SpecialFolderInternetCookies = 33 '(&H21)
const SpecialFolderInternetHistory = 34 '(&H22)
const SpecialFolderCommonApplicationData = 35 '(&H23)
const SpecialFolderWindowsFolder = 36 '(&H24)
const SpecialFolderSystemFolder = 37 '(&H25)
const SpecialFolderProgramFiles = 38 '(&H26)
const SpecialFolderMyPictures = 39 '(&H27)
const SpecialFolderProfile = 40 '(&H28)
const SpecialFolderCommonProgramFiles = 43 '(&H2B)
const SpecialFolderCommonDocumentTemplates = 45 '(&H2D)
const SpecialFolderCommonDocuments = 46 '(&H2E)
const SpecialFolderCommonAdminTools = 47 '(&H2F)
const SpecialFolderAdministrativeTools = 48 '(&H30)
const SpecialFolderCommonMyMusic = 53 '(&H35)
const SpecialFolderCommonMyPictures = 54 '(&H36)
const SpecialFolderCommonMyVideo = 55 '(&H37)
const SpecialFolderAllLocalDrives = -1 '(&HFFFFFFFF)
const SpecialFolderWindowsInstallDrive = -2 '(&HFFFFFFFE)
const SpecialFolderMyDocumentsDrive = -3 '(&HFFFFFFFD)
const SpecialFolderApplicationDataDrive = -4 '(&HFFFFFFFC)
const SpecialFolderNone = 0
More information on the Search Engine can be found here:
http://www.mythicsoft.com/filelocatorpro/sdkhelp/index.html