Directory Index and Search-VBForums
Results 1 to 2 of 2

Thread: Directory Index and Search

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    6

    Directory Index and Search

    Here is my script for indexing a directory (any directory, including network drives - should work with UNC paths too) and searching for matching files. In this version, it simply executes the files (i.e. opens them with their associated program), but you could customize the resultant actions to suit your needs.

    This was built as a homemade replacement to Locate32 (see http://lifehacker.com/397406/locate3...-and-reliably). The limitation of Locate32 and other desktop search apps (including Google desktop) is that the index is local to each user: as the number of users increases, so does the number of indexes and their load on the network server! With this script, there's only one shared index to maintain, and it's refreshed on a schedule you define.

    Code:
    'define search parameters
    const cintMinSearchLength = 3 'min length of search string
    const cintRefreshTime = 8 'hours before index will be refreshed
    const cintOpenMax = 24 'max number of files will attempt to open simultaneously
    const cintOpenWarn = 4 'max number of files will attempt to open before warning the user
    
    'define search parameters
    strSearchDir = "R:\Drawings"
    Set objShell = CreateObject("WScript.Shell")
    strIndexDir = objShell.CurrentDirectory
    strIndexFile = strIndexDir & "\Drawings.idx"
    strFilter = "*.pdf"
    strWindowTitle = "File Search v1.00"
    
    'ask user what drawing to open
    strSearch = InputBox("Please enter a partial filename:" & vbNewLine & vbNewLine & _
    				"Searches: " & strSearchDir, strWindowTitle)
    strSearch = Trim(strSearch)
    If Len(strSearch) < cintMinSearchLength Then
    	Wscript.Quit 0
    End If
    
    'query index file dates
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colFiles = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_Directory.Name='" & strIndexDir & "'} Where " _
            & "ResultClass = CIM_DataFile")
    
    'regenerate index file if too old
    'code credit: http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec07/hey1211.mspx
    strCurrentDate = Now
    boolFoundIndex = False
    For Each objFile In colFiles
    	strCurrentFile = objFile.Drive & objFile.Path & objFile.FileName & "." & objFile.Extension
    	If InStr(1,strCurrentFile,strIndexFile,1) Then
    	    strFileDate = WMIDateStringToDate(objFile.LastModified)
    	    intHours = DateDiff("h", strFileDate, strCurrentDate)
    	    'intMinutes = DateDiff("n", strFileDate, strCurrentDate)
    	    If intHours >= cintRefreshTime Then
    			RefreshIndex strSearchDir, strFilter, strIndexFile
    	    End If
    		boolFoundIndex = True
    	End If
    Next
    
    'generate index file if not found
    If Not boolFoundIndex Then
    	RefreshIndex strSearchDir, strFilter, strIndexFile
    End If
    
    'setup search to find desired part number in index files
    'cdoe credit http://www.microsoft.com/technet/scriptcenter/resources/qanda/may08/hey0505.mspx
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objRegExp = new RegExp
    With objRegExp
    	.Pattern = "^.*" & strSearch & ".*$"
    	.Global = True
    	.IgnoreCase = True
    	.Multiline = True
    End With
    
    'load index file
    Set objSearchIndex = objFSO.GetFile(strIndexFile)
    strContents = objFSO.OpenTextFile(strIndexFile, 1, false).Read(objSearchIndex.Size)
    
    'execute search
    Set objMatches = objRegExp.Execute(strContents)
    
    'open results for viewing
    If objMatches.Count > 0 Then
    	If objMatches.Count > cintOpenMax Then
    		'abort if too many matches found
    		intResult = MsgBox(objMatches.Count & " matches found." & vbNewLine & _
    						"Please try your search again." & vbNewLine & _
    						"(Program will now exit.)", 48, _
    						strWindowTitle)
    		Wscript.Quit 0
    	ElseIf objMatches.Count > cintOpenWarn Then
    		'warn user if opening more than a few matches
    		intResult = MsgBox(objMatches.Count & " matches found." & vbNewLine & _
    						"Are you sure you want to open all of these for viewing?", 4, _
    						strWindowTitle)
    		If intResult = 6 Then 'yes
    			For Each objMatch in objMatches
    				objShell.Run "cmd /c start """" """ & objMatch.Value & """", 0, False
    			Next
    		Else 'no
    			Wscript.Quit 0
    		End If
    	Else
    		'only a few matches, so go ahead and open them
    		For Each objMatch in objMatches
    			objShell.Run "cmd /c start """" """ & objMatch.Value & """", 0, False
    		Next
    	End If
    Else
    	MsgBox "No drawings found for """ & strSearch & """.", strWindowTitle
    End If
    
    
    'code credit: http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec07/hey1211.mspx
    Function WMIDateStringToDate(dtmInstallDate)
        WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
            Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
                & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
                    Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
                        13, 2))
    End Function
    
    Function RefreshIndex(strSearchDir, strFilter, strIndexFile)
    	objShell.Run "cmd /c echo Please wait while " & strSearchDir & _
    		" is indexed... & dir /b /s """ & strSearchDir & _
    		"\" & strFilter & """ > " & strIndexFile, 1, True
    End Function

  2. #2
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: Directory Index and Search

    Thanks for this. I've moved your code sample to the codebank forum for ASP/ASP.NET.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.