How to search by dates in the file system


This tip demonstrates how to set the name the search will be saved with; how to search for data by date; how to use the File System search engine to search a local disk; how to set the search so that it doesn't search subfolders; and how to run a search without showing the Search dialog box.
You could use a macro like this to define and execute a search that you run periodically. To end the search before it has finished, click the Search button on the Standard toolbar and then click Stop.

How to use

  1. Paste this macro into VBA.
  2. Change the path.
  3. Change the location where the search should begin.
  4. Run the macro.
[VBA]
Sub FileSystemDateRange()
    
    'create a new query object to define search criteria
    Dim pQ As IQuery
    Set pQ = New FileSystemQuery
    
    'set the name the search will be saved with in Search Results folder. If NameOfQuery
    'isn't set the search will be saved as "My Search".
    pQ.NameOfQuery = "metadata modified in first quarter"
    
    'search for documents whose metadata was modified within the specified date range.
    'FGDC and ISO metadata elements are examined; ESRI elements aren't used. For all other
    'esriFindDateOperators only Date1 is used.
    pQ.DateType = esriFindDateTypeMetadata
    pQ.DateOperator = esriFindDateOperatorDuring
    pQ.Date1 = "20030101" 'January 1, 2003
    pQ.Date2 = "20030331" 'March 31, 2003
    
    'get the Search dialog box object, then get the available search engines
    Dim i As Integer
    Dim pFD As IFindDialog
    Dim pSE As ISearchEngine
    Dim pSEP As ISearchEngineProperties
    Set pFD = New FindDialog
    For i = 0 To (pFD.GetNumSearchEngines - 1)
        Set pSE = pFD.getSearchEngine(i)
        Select Case pSE.Name
            
            Case "File system"
                'enable the File System search engine. This engine examines XML files on disk and
                'uses metadata content to evaluate search criteria. File-based data won't be found
                'if it doesn't have metadata, and data in geodatabases and ArcGIS servers can't
                'be searched. Because it doesn't open GxObjects, File System searches are faster
                'than Catalog searches.
                pSE.Enabled = True
                
                'search a local disk
                Set pSEP = pSE
                pSEP.LocationString = "C:\Test"
                
                'set property to prevent looking in subfolders. By default, File System searches
                'recursively look in folders within the starting location. Once this property has been
                'set, it won't change until it is set to something else. Without a recursive search,
                'coverages, grids, and tins won't be found.
                Dim pIFSQ As IFileSystemQuery
                Set pIFSQ = pSEP
                pIFSQ.IncludeSubFolders = False
                
                'NOTE: the FileSystemQuery object uses the IQuery interface. The
                'FileSystemXmlSearchEngine object uses the IFileSystemQuery interface.
                
            Case Else
                'disable other search engines
                pSE.Enabled = False
                
        End Select
    Next
    
    'initialize search components, then execute the search
    pFD.Show False
    pFD.DoSearch pQ
    
End Sub